Saya selalu berpikir bahwa menghasilkan halaman statis secara dinamis tidak mudah dilakukan. Saya melihatnya secara online kemarin dan saya pusing. Itu sebenarnya sangat sederhana. Idenya mungkin seperti ini.
1: Buat templat halaman HTML, dan gunakan string khusus untuk mewakili tempat yang ingin Anda tampilkan secara dinamis di halaman ini (seperti $ htmlstrstr $);
2: Dalam program, gunakan halaman HTML ini untuk membaca variabel string seperti STR;
3: Gunakan metode penggantian string untuk mengganti karakter khusus dengan apa yang Anda inginkan pada langkah pertama;
4Save;
Oke, sangat mudah, saya menulis kelas seperti ini di C# hari ini untuk menangani secara dinamis menghasilkan halaman HTML. Saya pikir saya masih menulisnya secara penuh. Saya baru saja melakukan kontak dengan .net, jadi saya berharap untuk memberikan nasihat.
Catatan: Kode dalam kategori ini tidak semuanya asli, beberapa kode merujuk pada kode netizen!
Berikut ini adalah kode untuk kelas konversi
Kode
1 menggunakan sistem;
2 using system.text;
3 Menggunakan Sistem.Web;
4 using system.configuration;
5using system.io;
6namespace Solucky
7 {
8/** //// <summary>
9 /// Ringkasan Deskripsi AspxtOhtml.
10 /// Catatan: Menggunakan kelas ini, Anda dapat mengonfigurasi kelas template di file web.config. Seperti yang ditunjukkan di bawah ini
11/** //*<AppSettings>
12 <Tambahkan key = "TemplateFilePath" value = "htmlmoudel.htm" />
13 <Tambahkan key = "htmlfilePath" value = "new/"> </ddd>
14 <Tambahkan key = "errlogpath" value = "aspxtoHtml_log.txt"> </ddd>
15 </appsettings>*/
16/** /// </summary>
17 Kelas Publik Aspxtohtml
18 {
19/** /// <summary>
20 /// Jumlah parameter yang akan diganti dalam file template
21 /// </summary>
22 private int _templateParamcount = 0;
23/** /// <summary>
24 /// jalur di mana file template berada
25 /// </summary>
26 Private String _templateFilePath = configurationsettings.appsettings ["TemplateFilePath"];
27/** /// <summary>
28 /// Path yang disimpan dalam file HTML yang dikonversi
29 /// </summary>
30 Private String _htmlfilePath = configurationsettings.appsettings ["htmlfilePath"];
31
32/** //// <summary>
33 /// Pengkodean Halaman Template
34 /// </summary>
35 Pengkodean Pribadi _templateHtMlCode = encoding.getEncoding ("GB2312");
36
37/** /// <summary>
38 /// pengkodean file yang dikonversi
39 /// </summary>
40 private encoding _code = encoding.getEncoding ("GB2312");
41
42/** //// <summary>
43 /// nama file html yang dikonversi
44 /// </summary>
45 string pribadi _convertedFileName = "";
46/** //// <summary>
47 /// Parameter dalam file template
48 /// </summary>
49 string pribadi [] _templateFileparameter;
50
51/** /// <summary>
52 /// Nilai aktual parameter dalam file ASPX harus diganti dengan file HTML
53 /// </summary>
54 Private String [] _aspxfileparameter;
55
56 Private String _errlogpath = configurationsettings.appsettings ["errlogpath"];
57
58 Atribut#Atribut Wilayah
59
60/** //// <summary>
61 /// Jumlah parameter yang akan diganti dalam file template
62 /// </summary>
63 TemplateParamCount publik
64 {
65 dapatkan
66 {
67 return this._templateParamCount;
68}
69 Set // Saat mengalokasikan jumlah parameter, array aktual dialokasikan untuk parameter dalam file template dan nilai aktual dalam file ASPX untuk menggantikan nilai aktual dalam file html.
70 {
71 if (nilai <0)
72 Lempar ArgumentException baru ();
73
74 if (nilai> 0)
75 {
76 this._templateParamCount = value;
77 // Parameter dalam file template
78 _templateFileParameter = string baru [nilai];
79 // Nilai aktual parameter dalam file ASPX harus diganti dengan file HTML
80 _aspxfileparameter = string baru [nilai];
81}
82 Lainnya
83 this._templateParamCount = 0;
84}
85}
86
87/** //// <summary>
88 /// jalur di mana file template berada
89 ///
90 /// </summary>
91 TemplateFilePath String Publik
92 {
93 dapatkan {return this._templateFilePath;}
94 set {this._templateFilePath = value;}
95}
96/** /// <summary>
97 /// Path yang disimpan dalam file HTML yang dikonversi
98 /// </summary>
99 string publik htmlfilepath
100 {
101 dapatkan {return this._htmlfilePath;}
102 set {this._htmlfilePath = value;}
103}
104
105/** /// <summary>
106 /// HTML Template File Encoding
107 /// </summary>
108 TemplateHtMlCode Publik
109 {
110 dapatkan {return this._templateHtMlCode;}
111 Set {this._templateHtMlCode = encoding.getEncoding (value.toString ());}
112}
113/** /// <summary>
114 /// Pengkodean
115 /// </summary>
116 Kode Pengkodean Publik
117 {
118 dapatkan {return this._code;}
119 Set {this._code = encoding.getEncoding (value.toString ());}
120}
121/** /// <summary>
122 /// jalur file kesalahan berada
123 /// </summary>
124 PUBLIK PUBLIK ERRLOGPATH
125 {
126 dapatkan {
127 if (! (This._errlogpath == null))
128 return this._errlogpath;
129 lainnya
130 return "aspxtohtml_log.txt";
131}
132 set {this._errlogpath = value;}
133}
134
135
136 #endregion
137
138 Operasi#Operasi Wilayah
139
140/** /// <summary>
141 /// Dapatkan jalur file relatif di mana file html yang dikonversi berada
142 /// misalnya: Jika htmlfilePath = "/news/"
143 /// Nama file HTML yang dikonversi adalah 2005050505.html
144 /// kemudian nilai yang dikembalikan adalah /news/2005050505.html
145 /// </summary>
146 /// <mendok Ulang> Return NULL jika properti ini dipanggil sebelum metode StartConvert disebut </membarks>
147 PUBLIK PUBLIK HTMLFILEVIRtualPath
148 {
149 dapatkan
150 {
151 if (! (This._convertedFileName == ""))
152 kembalikan this.htmlfilePath+this._convertedFileName;
153 lainnya
154 Return Null;
155}
156}
157
158/** /// <summary>
159 /// Bayar Nilai untuk Array Parameter Halaman HTML
160 /// </summary>
161 /// <param name = "param"> </param>
162 Public Void SetTemplateFileParameter (String [] param)
163 {
164 Coba
165 {
166 if (param.length == this.templateParamcount)
167 this._templateFileparameter = param;
168 // else // angkanya berbeda dari definisi asli
169 //
170}
171 Catch (System.Exception Ex)
172 {
173 WritErrfile (EX);
174}
175}
176/** /// <summary>
177 /// Bayar nilai untuk array parameter dalam file ASPX yang akan diganti dalam file HTML
178 /// </summary>
179 /// <param name = "param"> </param>
180 public void setapxfileparameter (string [] param)
181 {
182 Coba
183 {
184 if (param.length == this.templateParamcount)
185 this._aspxfileparameter = param;
186 // else // angkanya berbeda dari definisi asli
187 //
188}
189 Catch (System.Exception Ex)
190 {
191 WriteRfile (EX);
192}
193}
194/** /// <summary>
195 /// Mulai Konversi AspxtOHTML
196 /// </summary>
197 /// <Returns> Nilai pengembalian adalah nama file setelah pembuatan yang berhasil </eturns>
198 /// <womarks> Sebelum memanggil metode ini, perlu untuk memastikan bahwa metode settemplateFileParameter dan setaspxfileparameter telah dipanggil untuk operasi pembayaran nilai yang sesuai </komark>
199 String Publik 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 /// Mulai Konversi ASPXTOHTML
211 /// </summary>
212 /// <param name = "htmlparam"> Array dari semua parameter di halaman template html </param>
213 /// <param name = "aspxparam"> Halaman ASPX harus menggantikan array nilai parameter di halaman template html </param>
214 /// <Returns> Nilai pengembalian adalah nama file setelah pembuatan yang berhasil </returns>
215 String Publik StartConvert (String [] htmlparam, string [] aspxparam)
216 {
217 // Pertama hubungi Metode SetTemplatFileParameter dan SetaspxFileparameter untuk melakukan operasi pembayaran nilai
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 /// Gunakan waktu untuk menambahkan nomor acak untuk menghasilkan nama file
230 /// </summary>
231 /// <Returns> </returns>
232 Private String getFileName ()
233 {
234 // Gunakan waktu untuk menambahkan nomor acak untuk menghasilkan nama file
235 System.threading.thread.sleep (50);
236 string tahun = System.DateTime.now.year.toString ();
237 String bulan = string.Format ("{0: 0#}", System.DateTime.now.month);
238 String Daystr = String.Format ("{0: 0#}", System.DateTime.now.day);
239 string hourtr = 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 = System.Random () baru;
244 Pengembalian tahun + Bulan -Bulan + Hari -hari + Jam + Minutestr + SecondStr + MillionsEcondstr + String.Format ("{0: 0000#}", rd.next (100)) + ". Html";
245 // return datetime.now.tostring ("yyyymmddhhmmss")+". Html";
246}
247/** //// <summary>
248 /// Konversi
249 /// </summary>
250 /// <Returns> Mengembalikan nama file yang dinamai oleh Time </returns>
251 Private String WriteFile ()
252 {
253
254 // Baca file template
255 string temp = httpcontext.current.server.mappath (this.templateFilePath);
256 streamreader sr = null;
257 string str = "";
258 coba
259 {
260 sr = streamReader baru (temp, this.templateHtmlcode);
261 str = sr.readtoEnd (); // Baca file
262}
263 Catch (Exception Ex)
264 {
265 //httpcontext.current.response.write(exp.message);
266 //httpcontext.current.response.end ();
267 WritErrfile (EX);
268}
269 Akhirnya
270 {
271 sr.close ();
272}
273 // ganti konten
274 // Saat ini, file template telah dibaca ke variabel bernama STR
275 untuk (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 // Tulis file
292 StreamWriter SW = null;
293 coba
294 {
295
296 string path = httpcontext.current.server.mappath (this.htmlfilePath);
297 // nama file html
298 String htmlfileName = getFileName ();
299 SW = streamwriter baru (PATH + HTMLFileName, false, this.code);
300 Sw.write (str);
301 sw.flush ();
302 return htmlfilename;
303}
304 Catch (Exception Ex)
305 {
306 WritErrfile (EX);
307}
308 Akhirnya
309 {
310 sw.close ();
311}
312 return "";
313}
314
315/** /// <summary>
316 /// Lulus dalam URL untuk mengembalikan kode HTML ke halaman web
317 /// </summary>
318 /// <param name = "url"> url </param>
319 /// <Returns> </returns>
320 PUBLIK PUBLIK GETURLTOHTML (URL String)
321 {
322 coba
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 WritErrfile (EX);
334}
335 kembali "";
336}
337 #endregion
338
339
340 Konstruk #Region Construct
341
342 ASPXTOHTML PUBLIK ()
343 {
344 //
345 // TODO: Tambahkan logika konstruktor di sini
346 //
347}
348
349 Private Void SettemplateParamCount (int TemplateParamCount)
350 {
351 if (TemplateParamCount> 0)
352 this.templateParamCount = TemplateParamCount;
353}
354/** //// <summary>
355 /// Berikan jumlah parameter yang akan diganti
356 /// </summary>
357 /// <param name = "TemplateParamCount"> </param>
358 ASPXTOHTML PUBLIK (INT TemplateParamCount)
359 {
360 SetTemplateParamCount (TemplateParamCount);
361
362}
363/** //// <summary>
364 ///
365 /// </summary>
366 /// <param name = "TemplateParamCount"> Jumlah parameter di halaman template HTML </param>
367 /// <param name = "htmlfilePath"> Jalur folder dari file html yang dihasilkan </param>
368 /// <param name = "templateFilePath"> path halaman template html </param>
369 Publik 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 /// Tulis kesalahan ke metode file #region menulis kesalahan ke metode file
382 /// </summary>
383 /// <param name = "ee"> </param>
384 Private Void WriterRfile (Exception EE)
385 {
386
387 FileStream FS1 = FileStream baru (httpcontext.current.server.mappath (errlogpath), system.io.filemode.append);
388 StreamWriter SW1 = StreamWriter baru (FS1);
389 SW1.WriteLine ("***************************************************************************);
390 SW1.WriteLine ("Tanggal Kesalahan:" + System.DateTime.now);
391 SW1.WriteLine ("Deskripsi Kesalahan:" + EE.Message);
392 SW1.WriteLine ("Nama Kesalahan:" + EE.Source);
393 SW1.WriteLine ("Detail:" + ee.tostring ());
394 SW1.WriteLine ("**********************************************************************);
395 sw1.close ();
396}
397 #endregion
398}
399}
400