ASP.NET生成静态页面的方法
使用ASP.NET生成静态页面的方法有两种,第一种是使用C#在后台硬编码,第二种是读取模板文件,使用字符串替换的方法。第一种方法编码量大,而且维护比较困难。我重点讲解第二种方法。第二种方法的基本思路是:使用DW之类的工具生成一个静态页面模板。读取该模板文件,然后对里面的特殊标记使用真实的数据替换掉,并生成一个HTML文件
请看代码
1.C#
1
using System;
2
using System.Collections.Generic;
3
using System.Text;
4
using System.Xml;
5
using System.IO;
6![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
namespace htmlWeb
8
{
9
public class CreateHtm
10
{
11
12![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
private string fileName;
14![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
64
fileName = DateTime.Now.ToFileTime().ToString() + ".htm";
65![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
74![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
75
}
76
catch (Exception ex)
77
{
78
throw ex;
79
}
80![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
81![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
82
}
83![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
84
public void DeleteHtml(String dirName)
85
{
86
File.Delete(dirName);
87
}
88
}
89
}
90![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
30
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
31
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
32
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
33
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
34
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
35
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
36
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
37
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
38
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
39
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
40
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
41
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
42
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
43
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
44
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
45
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
46
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
47
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
48
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
49
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
50
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
51
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
52
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
53
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
54
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
55
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
56
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
57
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
58
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
59
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
60
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
61
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
62
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
63
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
64
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
65
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
66
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
67
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
68
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
69
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
70
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
71
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
72
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
73
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
74
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
75
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
76
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
77
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
78
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
79
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
80
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
81
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
82
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
83
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
84
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
85
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
86
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
87
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
88
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
89
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
90
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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.此文件告诉使用者每个标记的含义。如下
1
<?xml version="1.0" encoding="utf-8" ?>
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![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
这样配置后,类会把标记数组0,1,2,3的位置分别替换为题目,作者,内容,发布日期。![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
看模板文件
1
<head>
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>
使用方法:![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
1
using System;
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![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
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![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
朋友们只要把相应的参数修改为实际的值,就生成静态文件了。![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
2
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
3
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
5
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
6
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
7
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
8
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
9
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
10
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
11
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
12
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
13
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
14
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
15
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
16
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
17
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
19
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
20
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
21
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
22
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
23
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
24
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
25
![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
27
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockEnd.gif)
28
![](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockEnd.gif)
29
![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)