asp.net生成静态页面原理

1  using  System;
 2  using  System.Collections;
 3  using  System.ComponentModel;
 4  using  System.Data;
 5  using  System.Drawing;
 6  using  System.Web;
 7  using  System.Web.SessionState;
 8  using  System.Web.UI;
 9  using  System.Web.UI.WebControls;
10  using  System.Web.UI.HtmlControls;
11  using  System.IO;
12  using  System.Text;
13 
14  namespace  menutest
15  {
16    ///   <summary>
17    ///  Conn 的摘要说明。
18    ///   </summary>
19    //  by kyo
20    //  此类是生成静态网页的小程序  
21    public   class  Conn
22   {
23     public  Conn()
24    {
25    
26    }
27     public   static   bool  WriteFile( string  strText, string  strContent, string  strAuthor)
28    {
29      string  path  =  HttpContext.Current.Server.MapPath( " /menutest/ " );
30     Encoding code  =  Encoding.GetEncoding( " gb2312 " );
31      //  读取模板文件
32      string  temp  =  HttpContext.Current.Server.MapPath( " /menutest/news/text.html " );
33     StreamReader sr = null ;
34     StreamWriter sw = null ;
35      string  str = ""
36      try
37     {
38      sr  =   new  StreamReader(temp,code);
39      str  =  sr.ReadToEnd();  //  读取文件
40     }
41      catch (Exception exp)
42     {
43      HttpContext.Current.Response.Write(exp.Message);
44      HttpContext.Current.Response.End();
45      sr.Close();
46     }
47 
48 
49      // string htmlfilename=DateTime.Now.ToString("yyyyMMddHHmmss")+".html";
50      string  htmlfilename = " kyo.html " ;
51      //  替换内容
52      //  这时,模板文件已经读入到名称为str的变量中了
53     str  =  str.Replace( " ShowArticle " ,strText);  // 模板页中的ShowArticle
54     str  =  str.Replace( " biaoti " ,strText);
55     str  =  str.Replace( " content " ,strContent);
56     str  =  str.Replace( " author " ,strAuthor);
57      //  写文件
58      try
59     {
60      sw  =   new  StreamWriter(path  +  htmlfilename ,  false , code);
61      sw.Write(str);
62      sw.Flush();
63     }
64      catch (Exception ex)
65     {
66      HttpContext.Current.Response.Write(ex.Message);
67      HttpContext.Current.Response.End();
68     }
69      finally
70     {
71      sw.Close();
72     }
73      return   true ;
74    }
75   }
76  }
77  // 原理是利用System.IO中的类读写模板文件,然后用Replace替换掉模板中的标签,写入静态html.
78  
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值