一直以为动态生成静态页面不好做,昨天在网上找了下,我晕,其实很简单,思路大概是这样的,
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);
192 }
193 }
194 /**//// <summary>
195 /// 开始进行aspxtohtml转换
196 /// </summary>
197 /// <returns>返回值为成功创建后的文件名称</returns>
198 /// <remarks>在调用此方法之前必需确定已调用settemplatefileparameter 和setaspxfileparameter方法进行相应的付值操作</remarks>
199 public string startconvert()
200 {
201 if(this._templatefileparameter.length==this._aspxfileparameter.length)
202 {
203 return writefile();
204 }
205 else{
206 return null;
207 }
208 }
209 /**//// <summary>
210 /// 开始进行aspxtohtml转换
211 /// </summary>
212 /// <param name="htmlparam">html模板页中的所有参数数组</param>
213 /// <param name="aspxparam">aspx页面中要代替html模板页中参数值数组</param>
214 /// <returns>返回值为成功创建后的文件名称</returns>
215 public string startconvert(string[] htmlparam,string[] aspxparam)
216 {
217 //先调用settemplatefileparameter 和setaspxfileparameter方法,进行付值操作
218 settemplatefileparameter(htmlparam);
219 setaspxfileparameter(aspxparam);
220 //
221 string fn=this.startconvert();
222 //
223 _convertedfilename=fn;
224 //
225 return fn;
226 }
227
228 /**//// <summary>
229 /// 用时间加随机数生成一个文件名
230 /// </summary>
231 /// <returns></returns>
232 private string getfilename()
233 {
234 //用时间加随机数生成一个文件名
235 system.threading.thread.sleep(50);
236 string yearstr = system.datetime.now.year.tostring();
237 string monthstr = string.format("{0:0#}",system.datetime.now.month);
238 string daystr = string.format("{0:0#}",system.datetime.now.day);
239 string hourstr = string.format("{0:0#}",system.datetime.now.hour);
240 string minutestr = string.format("{0:0#}",system.datetime.now.minute);
241 string secondstr = string.format("{0:0#}",system.datetime.now.second);
242 string millisecondstr = string.format("{0:000#}",system.datetime.now.millisecond);
243 system.random rd = new system.random();
244 return yearstr + monthstr + daystr + hourstr + minutestr + secondstr + millisecondstr + string.format("{0:0000#}",rd.next(100))+".html";
245 //return datetime.now.tostring("yyyymmddhhmmss")+".html";
246 }
247 /**//// <summary>
248 /// 进行转换处理
249 /// </summary>
250 /// <returns>返回以时间命名的文件名</returns>
251 private string writefile()
252 {
253
254 // 读取模板文件
255 string temp = httpcontext.current.server.mappath(this.templatefilepath);
256 streamreader sr=null;
257 string str="";
258 try
259 {
260 sr = new streamreader(temp, this.templatehtmlcode);
261 str = sr.readtoend(); // 读取文件
262 }
263 catch(exception ex)
264 {
265 //httpcontext.current.response.write(exp.message);
266 //httpcontext.current.response.end();
267 writeerrfile(ex);
268 }
269 finally
270 {
271 sr.close();
272 }
273 // 替换内容
274 // 这时,模板文件已经读入到名称为str的变量中了
275 for(int i=0;i<this.templateparamcount;i++)
276 {
277 str =str.replace(this._templatefileparameter[i],this._aspxfileparameter[i]);
278 }
279
280 return savefile(str);
281 }
282
283 /**//// <summary>
284 ///
285 /// </summary>
286 /// <param name="str"></param>
287 /// <returns></returns>
288
289 private string savefile(string str)
290 {
291 // 写文件
292 streamwriter sw=null;
293 try
294 {
295
296 string path = httpcontext.current.server.mappath(this.htmlfilepath);
297 //html文件名称
298 string htmlfilename=getfilename();
299 sw = new streamwriter(path + htmlfilename , false, this.code);
300 sw.write(str);
301 sw.flush();
302 return htmlfilename;
303 }
304 catch(exception ex)
305 {
306 writeerrfile(ex);
307 }
308 finally
309 {
310 sw.close();
311 }
312 return "";
313 }
314
315 /**//// <summary>
316 /// 传入url返回网页的html代码
317 /// </summary>
318 /// <param name="url">url</param>
319 /// <returns></returns>
320 public string geturltohtml(string url)
321 {
322 try
323 {
324 system.net.webrequest wreq = system.net.webrequest.create(url);
325 system.net.webresponse wresp =wreq.getresponse();
326 system.io.stream respstream = wresp.getresponsestream();
327 system.io.streamreader reader = new system.io.streamreader(respstream, system.text.encoding.getencoding("gb2312"));
328 return savefile(reader.readtoend());
329
330 }
331 catch(system.exception ex)
332 {
333 writeerrfile(ex);
334 }
335 return "";
336 }
337 #endregion
338
339
340 构造#region 构造
341
342 public aspxtohtml()
343 {
344 //
345 // todo: 在此处添加构造函数逻辑
346 //
347 }
348
349 private void settemplateparamcount(int templateparamcount)
350 {
351 if (templateparamcount>0)
352 this.templateparamcount=templateparamcount;
353 }
354 /**//// <summary>
355 /// 提供欲代替的参数个数
356 /// </summary>
357 /// <param name="templateparamcount"></param>
358 public aspxtohtml(int templateparamcount)
359 {
360 settemplateparamcount(templateparamcount);
361
362 }
363 /**//// <summary>
364 ///
365 /// </summary>
366 /// <param name="templateparamcount">html模板页中的参数个数</param>
367 /// <param name="htmlfilepath">生成的html文件所存放的文件夹路径</param>
368 /// <param name="templatefilepath">html模板页路径</param>
369 public aspxtohtml(int templateparamcount,string htmlfilepath,string templatefilepath)
370 {
371 settemplateparamcount(templateparamcount);
372 this.htmlfilepath = htmlfilepath;
373 this.templatefilepath = templatefilepath;
374
375 }
376 #endregion
377
378 #region
379
380 /**//// <summary>
381 /// 把错误写入文件方法#region 把错误写入文件方法
382 /// </summary>
383 /// <param name="ee"></param>
384 private void writeerrfile(exception ee)
385 {
386
387 filestream fs1 = new filestream(httpcontext.current.server.mappath(errlogpath), system.io.filemode.append);
388 streamwriter sw1 = new streamwriter(fs1);
389 sw1.writeline("**************************************************");
390 sw1.writeline("错误日期:" + system.datetime.now);
391 sw1.writeline("错误描述:" + ee.message);
392 sw1.writeline("错误名称:" + ee.source);
393 sw1.writeline("详细:" + ee.tostring());
394 sw1.writeline("*************************************************");
395 sw1.close();
396 }
397 #endregion
398 }
399}
400
新闻热点
疑难解答