您的位置:首页 > 编程语言 > ASP

asp.net生成静态页面原理

2006-12-06 14:12 302 查看
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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: