第一种方法:直接抓取asp.net的http输出流写入htm文件中
public void toHtml(string fileName)
{
StringWriter wr = new StringWriter();
string newPath = Server.MapPath(fileName.Replace("aspx", "htm"));
Server.Execute(fileName, wr);
txbDiary.Text +="\r\n "+System.DateTime.Now.ToString()+" "+ fileName + " 更新成功!";
File.WriteAllText(newPath, wr.ToString(),System.Text.Encoding.GetEncoding("gb2312"));
}
下面是转载的方法:
使用ASP.NET生成静态页面的方法有两种,第一种是使用C#在后台硬编码,第二种是读取模板文件,使用字符串替换的方法。第一种方法编码量大,而且维护比较困难。我重点讲解第二种方法。第二种方法的基本思路是:使用DW之类的工具生成一个静态页面模板。读取该模板文件,然后对里面的特殊标记使用真实的数据替换掉,并生成一个HTML文件
请看代码
1.C#
2 using System.Collections.Generic;
3 using System.Text;
4 using System.Xml;
5 using System.IO;
6
7 namespace htmlWeb
8 {
9 public class CreateHtm
10 {
11
12
13 private string fileName;
14
15 public String FileName
16 {
17 get { return fileName; }
18 }
19 /// <summary>
20 /// 读取配置文件
21 /// </summary>
22 /// <param name="dirName">配置文件的路径名</param>
23 /// <param name="tag">配置文件中的标签名</param>
24 /// <returns>_replaceStr的长度</returns>
25 private int GetConfig(String dirName, String tag)
26 {
27 XmlDataDocument config = new XmlDataDocument();
28 try
29 {
30 config.Load(dirName);
31 }
32 catch (Exception ex)
33 {
34 throw ex;
35 }
36 XmlNodeList list = config.GetElementsByTagName(tag);
37 return list.Count;
38 }
39 /// <summary>
40 ///生成HTML文件
41 /// </summary>
42 /// <param name="configFileName">配置文件的路径名</param>
43 /// <param name="configTag">配置文件中的标签名</param>
44 /// <param name="dir">生成文件所在的文件夹的路径</param>
45 /// <param name="templateFile">模板文件的的路径</param>
46 /// <param name="param">要替换的字符串数组</param>
47 /// <returns>生成的文件名</returns>
48 public void MakeHtml(String configFileName, String configTag, String dir, String templateFile, String[] param)
49 {
50 fileName = null;
51 int count = GetConfig(configFileName, configTag);
52 String[] _replaceStr = new String[count];
53 try
54 {
55 FileStream tFile = File.Open(templateFile, FileMode.Open, FileAccess.Read);
56 StreamReader reader = new StreamReader(tFile, Encoding.GetEncoding("gb2312"));
57 StringBuilder sb = new StringBuilder(reader.ReadToEnd());
58 reader.Close();
59 for (int i = 0; i < count; i++)
60 {
61 sb.Replace("$repalce[" + i + "]$", param[i]);
62 }
63
64 fileName = DateTime.Now.ToFileTime().ToString() + ".htm";
65
66 FileStream rFile = File.Create(dir+"/" + fileName);
67 StreamWriter writer = new StreamWriter(rFile, Encoding.GetEncoding("gb2312"));
68 writer.Write(sb.ToString());
69 writer.Flush();
70 writer.Close();
71
72
73
74
75 }
76 catch (Exception ex)
77 {
78 throw ex;
79 }
80
81
82 }
83
84 public void DeleteHtml(String dirName)
85 {
86 File.Delete(dirName);
87 }
88 }
89}
90
private int GetConfig(String dirName, String tag) 此方法用于读取配置文件(见后),主要是获得要替换的字符串的个数,在本类同体现为一个字符串数组
public void MakeHtml(String configFileName, String configTag, String dir, String templateFile, String[] param) 此方法用于生成静态页面
51.52行创建一个字符数组,数组长度为配置文件中的节点个数。55-58行读取模板文件,并用读到的模板文件的HTML代码生成一个StringBuilder对象。59-62行使用StringBuilderd对象的repalce()方法替换标记“$repalce[i]$"为真实的数据
64行生成一个唯一的文件名(防止覆盖)66-70行把新的字符串写到文件中。这样就生成了一个静态文件了。
下面看一个使用的实例:
一个文章管理系统,利用这个类来生成静态页面。
首先,建立一个配置文件 config.xml.此文件告诉使用者每个标记的含义。如下
2 < htmlWeb version ="1" >
3 < config >
4 < article key ="0" value ="title" />
5 < article key ="1" value ="author" />
6 < article key ="2" value ="context" />
7 < aritcle key ="3" value ="date" />
8 </ config >
9 </ htmlWeb >
10
看模板文件
2 < title >模板文件 </ title >
3 </ head >
4 < body >
5 < h1 >这是一个简单的HTML页,朋友们可以根据自己的需要重新设计 </ h1 >
6 < li >标题:$replace[0]$ </ li >
7 < li >作者:$replace[1]$ </ li >
8 < li >内容:$repalce[2]$ </ li >
9 < li >时间:$repalce[3]$ </ li >
10 </ body >
2 using System.Data;
3 using System.Configuration;
4 using System.Web;
5 using System.Web.Security;
6 using System.Web.UI;
7 using System.Web.UI.WebControls;
8 using System.Web.UI.WebControls.WebParts;
9 using System.Web.UI.HtmlControls;
10
11 namespace UseT
12 {
13 public class Test{
14
15 public void main(){
16 string[] param = new string[4];
17 param[0] = "测试模板";
18 param[1] = "农佳捷";
19 param[2] = "这是一个测试文章";
20 param[3] = "2007-10-30";
21
22 htmlWeb.CreateHtm cs = new htmlWeb.CreateHtm();
23 cs.MakeHtml("配置文件的路径
24“, ”article“, ”生成文件的路径“, "模板文件的路径", param)
25
26 }
27 }
28}
29