I always thought that dynamically generating static pages is not easy to do. I looked it online yesterday and I was dizzy. It was actually very simple. The idea is probably like this.
1: Create an html page template, and use a special string to represent the places you want to display dynamically in this page (such as $htmlstrstr$);
2: In the program, use this html page to read a string variable such as str;
3: Use the string's replacement method to replace the special characters with what you want in the first step;
4Save;
OK, so easy, I wrote a class like this in C# today to handle dynamically generating html pages. I thought I was still writing it in full. I just came into contact with .net, so I hope to give advice.
Note: The codes in this category are not all original, some codes refer to the codes of netizens!
The following is the code for the conversion class
Code
1using system;
2using system.text;
3using system.web;
4using system.configuration;
5using system.io;
6namespace solucky
7{
8 /**//// <summary>
9 /// Summary description of aspxtohtml.
10 /// Note: Using this class, you can configure the template class in the web.config file. As shown below
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 /// The number of parameters to be replaced in the template file
21 /// </summary>
22 private int _templateparamcount=0;
23 /**/// <summary>
24 /// The path where the template file is located
25 /// </summary>
26 private string _templatefilepath = configurationsettings.appsettings["templatefilepath"];
27 /**/// <summary>
28 /// The path stored in the converted html file
29 /// </summary>
30 private string _htmlfilepath = configurationsettings.appsettings["htmlfilepath"];
31
32 /**//// <summary>
33 /// Template page encoding
34 /// </summary>
35 private encoding _templatehtmlcode =encoding.getencoding("gb2312");
36
37 /**/// <summary>
38 /// Converted file encoding
39 /// </summary>
40 private encoding _code = encoding.getencoding("gb2312");
41
42 /**//// <summary>
43 /// Converted html file name
44 /// </summary>
45 private string _convertedfilename="";
46 /**//// <summary>
47 /// Parameters in the template file
48 /// </summary>
49 private string[] _templatefileparameter;
50
51 /**/// <summary>
52 /// The actual value of the parameter in the aspx file should be replaced by the html file
53 /// </summary>
54 private string[] _aspxfileparameter;
55
56 private string _errlogpath = configurationsettings.appsettings["errlogpath"];
57
58 Attributes#region Attributes
59
60 /**//// <summary>
61 /// The number of parameters to be replaced in the template file
62 /// </summary>
63 public int templateparamcount
64 {
65 get
66 {
67 return this._templateparamcount;
68 }
69 set// When allocating the number of parameters, the actual array is allocated for the parameters in the template file and the actual values in the aspx file to replace the actual values in the html file.
70 {
71 if (value < 0)
72 throw new argumentexception();
73
74 if(value>0)
75 {
76 this._templateparamcount=value;
77 // Parameters in the template file
78 _templatefileparameter = new string[value];
79 //The actual value of the parameter in the aspx file should be replaced by the html file
80 _aspxfileparameter = new string[value];
81 }
82 else
83 this._templateparamcount=0;
84 }
85 }
86
87 /**//// <summary>
88 /// The path where the template file is located
89 ///
90 /// </summary>
91 public string templatefilepath
92 {
93 get{ return this._templatefilepath;}
94 set{ this._templatefilepath=value;}
95 }
96 /**/// <summary>
97 /// The path stored in the converted html file
98 /// </summary>
99 public string htmlfilepath
100 {
101 get{ return this._htmlfilepath;}
102 set{ this._htmlfilepath=value;}
103 }
104
105 /**/// <summary>
106 /// html template file encoding
107 /// </summary>
108 public encoding templatehtmlcode
109 {
110 get{ return this._templatehtmlcode;}
111 set{ this._templatehtmlcode=encoding.getencoding(value.tostring());}
112 }
113 /**/// <summary>
114 /// Encoding
115 /// </summary>
116 public encoding code
117 {
118 get{ return this._code;}
119 set{ this._code=encoding.getencoding(value.tostring());}
120 }
121 /**/// <summary>
122 /// The path of the error file is located
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 Operation#region Operation
139
140 /**/// <summary>
141 /// Get the relative file path where the converted html file is located
142 /// For example: if htmlfilepath="/news/"
143 /// The converted html file name is 2005050505.html
144 /// Then the returned value is /news/200505050505.html
145 /// </summary>
146 /// <remarks> Return null if this property is called before the startconvert method is called</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 /// Pay value for the html page parameter array
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//The number is different from the original definition
169 //
170 }
171 catch(system.exception ex)
172 {
173 writeerrfile(ex);
174 }
175 }
176 /**/// <summary>
177 /// Pay the value for the parameter array in the aspx file that will be replaced in the html file
178 /// </summary>
179 /// <param name="param"></param>
180 public void setapxfileparameter(string[] param)
181 {
182 try
183 {
184 if(param.length==this.templateparamcount)
185 this._aspxfileparameter=param;
186 //else//The number is different from the original definition
187 //
188 }
189 catch(system.exception ex)
190 {
191 writeerrfile(ex);
192 }
193 }
194 /**/// <summary>
195 /// Start aspxtohtml conversion
196 /// </summary>
197 /// <returns>Return value is the file name after successful creation</returns>
198 /// <remarks>Before calling this method, it is necessary to make sure that the settemplatefileparameter and setaspxfileparameter methods have been called for corresponding value-paying operations</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 /// Start aspxtohtml conversion
211 /// </summary>
212 /// <param name="htmlparam">Array of all parameters in html template page</param>
213 /// <param name="aspxparam">The aspx page should replace the parameter value array in the html template page</param>
214 /// <returns>Return value is the file name after successful creation</returns>
215 public string startconvert(string[] htmlparam,string[] aspxparam)
216 {
217 //First call the settemplatefileparameter and setaspxfileparameter methods to perform value payment operations
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 /// Use time to add random numbers to generate a file name
230 /// </summary>
231 /// <returns></returns>
232 private string getfilename()
233 {
234 // Use time to add random numbers to generate a file name
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 + millionsecondstr + string.format("{0:0000#}",rd.next(100))+".html";
245 //return datetime.now.tostring("yyyymmddhhmmss")+".html";
246 }
247 /**//// <summary>
248 /// Convert
249 /// </summary>
250 /// <returns>Returns the file name named by time</returns>
251 private string writefile()
252 {
253
254 // Read template file
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(); // Read the file
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 // Replace content
274 // At this time, the template file has been read into the variable named 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 // Write a file
292 streamwriter sw=null;
293 try
294 {
295
296 string path = httpcontext.current.server.mappath(this.htmlfilepath);
297 //html file name
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 /// Pass in url to return the html code to the web page
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 Construct #region Construct
341
342 public aspxtohtml()
343 {
344 //
345 // todo: Add constructor logic here
346 //
347 }
348
349 private void settemplateparamcount(int templateparamcount)
350 {
351 if (templateparamcount>0)
352 this.templateparamcount=templateparamcount;
353 }
354 /**//// <summary>
355 /// Provide the number of parameters to be replaced
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">Number of parameters in the html template page</param>
367 /// <param name="htmlfilepath">The folder path of the generated html file</param>
368 /// <param name="templatefilepath">html template page path</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 /// Write errors to file method #region Write errors to file method
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("Error date:" + system.datetime.now);
391 sw1.writeline("Error description:" + ee.message);
392 sw1.writeline("Error name:" + ee.source);
393 sw1.writeline("Details:" + ee.tostring());
394 sw1.writeline("******************************************************************************);
395 sw1.close();
396 }
397 #endregion
398 }
399}
400