📄 根据html页面模板动态生成html页面(c#类).txt
字号:
根据html页面模板动态生成html页面(c#类)
发布作者:佚名 发布时间:2006-9-9 被点击48次
--------------------------------------------------------------------------------
一直以为动态生成静态页面不好做,昨天在网上找了下,我晕,其实很简单,思路大概是这样的,
1:建立一个html页面模板,在这个页面中把你想要动态显示的地方用特殊的字符串表示(如
$htmlstrstr$);
2:在程序中用将这个html页面读到一个字符串变量如str;
3:用字符串的resplace方法将在第一步中特殊字符替换成你想要的内容;
4保存;
OK,so easy,今天就用C#写了一个这样的类,用来处理动态生成html页面的,自认为还写的完
整,刚接触.NET不久,望指教
注:此类中的代码不全是原创,部份代码参照网友的代码!
以下是转换类的代码
代码
1using System;
2using System.Text;
3using System.Web;
4using System.Configuration;
5using System.IO;
6namespace solucky
7{
8 /**//// <summary>
9 /// AspxToHtml 的摘要说明。
10 /// 注:使用此类,你可以在web.config文件对模板类进行配置.如下
11 /**//*<appSettings>
12 <add key="templateFilePath" value="htmlmoudel.htm" />
13 <add key="htmlFilePath" value="new/"></add>
14 <add key="ErrLogPath" value="aspxTohtml_log.txt"></add>
15 </appSettings>*/
16 /**//// </summary>
17 public class AspxToHtml
18 {
19 /**//// <summary>
20 /// 模板文件中要替代的参数个数
21 /// </summary>
22 private int _templateParamCount=0;
23 /**//// <summary>
24 /// 模板文件所在的路径
25 /// </summary>
26 private string _templateFilePath
=ConfigurationSettings.AppSettings["templateFilePath"];
27 /**//// <summary>
28 /// 转换后的html文件所存放的路径
29 /// </summary>
30 private string _htmlFilePath
=ConfigurationSettings.AppSettings["htmlFilePath"];
31
32 /**//// <summary>
33 /// 模板页页面编码
34 /// </summary>
35 private Encoding _templateHtmlCode
=Encoding.GetEncoding("gb2312");
36
37 /**//// <summary>
38 /// 转换后的文件编码
39 /// </summary>
40 private Encoding _code = Encoding.GetEncoding("gb2312");
41
42 /**//// <summary>
43 /// 转换后的html文件名
44 /// </summary>
45 private string _convertedFilename="";
46 /**//// <summary>
47 /// 模板文件中的参数
48 /// </summary>
49 private string[] _templateFileparameter ;
50
51 /**//// <summary>
52 /// aspx文件中的要代替HTML文件中的参数实际值
53 /// </summary>
54 private string[] _aspxFileparameter;
55
56 private string _errlogPath = ConfigurationSettings.AppSettings["ErrLogPath"];
57
58 属性#region 属性
59
60 /**//// <summary>
61 /// 模板文件中要替代的参数个数
62 /// </summary>
63 public int TemplateParamCount
64 {
65 get
66 {
67 return this._templateParamCount;
68 }
69 set//分配参数个数时,同时为模板文件中的参数和aspx文件中的要代替
HTML文件中的参数实际值这两个分配实际数组
70 {
71 if (value < 0)
72 throw new ArgumentException();
73
74 if(value>0)
75 {
76 this._templateParamCount=value;
77 //模板文件中的参数
78 _templateFileparameter = new string[value];
79 //aspx文件中的要代替HTML文件中的参数实际值
80 _aspxFileparameter = new string[value];
81 }
82 else
83 this._templateParamCount=0;
84 }
85 }
86
87 /**//// <summary>
88 /// 模板文件所在的路径
89 ///
90 /// </summary>
91 public string TemplateFilePath
92 {
93 get{ return this._templateFilePath;}
94 set{ this._templateFilePath=value;}
95 }
96 /**//// <summary>
97 /// 转换后的html文件所存放的路径
98 /// </summary>
99 public string HtmlFilePath
100 {
101 get{ return this._htmlFilePath;}
102 set{ this._htmlFilePath=value;}
103 }
104
105 /**//// <summary>
106 /// html模板文件编码
107 /// </summary>
108 public Encoding TemplateHtmlCode
109 {
110 get{ return this._templateHtmlCode;}
111 set{ this._templateHtmlCode=Encoding.GetEncoding(value.ToString());}
112 }
113 /**//// <summary>
114 /// 编码
115 /// </summary>
116 public Encoding Code
117 {
118 get{ return this._code;}
119 set{ this._code=Encoding.GetEncoding(value.ToString());}
120 }
121 /**//// <summary>
122 /// 错误文件所在路径
123 /// </summary>
124 public string ErrLogPath
125 {
126 get{
127 if(!(this._errlogPath==null))
128 return this._errlogPath;
129 else
130 return "aspxTohtml_log.txt";
131 }
132 set{this._errlogPath=value;}
133 }
134
135
136 #endregion
137
138 操作#region 操作
139
140 /**//// <summary>
141 /// 获取转换后的html文件所在相对文件路径
142 /// 如:如果HtmlFilePath="/news/"
143 /// 转换后的html文件名为200505050505.html
144 /// 则返回的值为/news/200505050505.html
145 /// </summary>
146 /// <remarks>如果在未调用StartConvert方法之前调用此属性则返回
null</remarks>
147 public string HtmlFileVirtualPath
148 {
149 get
150 {
151 if(!(this._convertedFilename==""))
152 return this.HtmlFilePath+this._convertedFilename;
153 else
154 return null;
155 }
156 }
157
158 /**//// <summary>
159 /// 为HTML页面参数数组付值
160 /// </summary>
161 /// <param name="param"></param>
162 public void setTemplateFileparameter(string[] param)
163 {
164 try
165 {
166 if(param.Length==this.TemplateParamCount)
167 this._templateFileparameter=param;
168 //else//与原定义的个数不等
169 //
170 }
171 catch(System.Exception ex)
172 {
173 WriteErrFile(ex);
174 }
175 }
176 /**//// <summary>
177 /// 为aspx文件中将要替换html文件中的参数数组付值
178 /// </summary>
179 /// <param name="param"></param>
180 public void setAspxFileparameter(string[] param)
181 {
182 try
183 {
184 if(param.Length==this.TemplateParamCount)
185 this._aspxFileparameter=param;
186 //else//与原定义的个数不等
187 //
188 }
189 catch(System.Exception ex)
190 {
191 WriteErrFile(ex);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -