生成静态页面

来源:互联网 发布:js画正方形 编辑:程序博客网 时间:2024/06/09 13:43

很早就想知道静态页面是怎么生成的,今天在网上看到了一篇文章,讲的就是生成静态页面,自已改了改,呵呵,终于成功了。

以下是代码

private void writeFile()
  {

   string path=Request.PhysicalApplicationPath;
   System.Text.Encoding en=System.Text.Encoding.GetEncoding("gb2312");

   //读模板
   string temp=Request.PhysicalApplicationPath+("a.htm");
   StreamReader sr=null;
   StreamWriter sw=null;
   string str="";

   try
   {
    sr=new StreamReader(temp,en);
    str=sr.ReadToEnd();
   }
   catch
   {
    Response.Write("error");
    sr.Close();
   }

   string  filename=System.DateTime.Now.ToString("yyyyMMddHHmmss")+".htm";

   str=str.Replace("content",this.TextBox1.Text);
   try
   {
    sw=new StreamWriter(path+filename,false,en);
    sw.Write(str);
    sw.Flush();
   }
   catch
   {
    Response.Write("写入错误");
    Response.End();
   }
   finally
   {
    sw.Close();
   }
   Response.Write("生成成功");
   
  }

窗体上放一个 TextBox 就行了,外加一个按纽,还需要做的是,把要写的目录的属性改一下,选中 everyone 这个用户让其可以写入、修改就行了

 

点按纽,OK ,成功了,一个静态页面就这样生成了,很简单的,如果再和数据库连上,来保存生成的静态页面的文件名,就可以做一个简单的新闻发布系统了