静的ページを動的に生成するのは簡単ではないといつも思っていました。昨日オンラインで見て、めまいがしました。実際にはとてもシンプルでした。アイデアはおそらくこのようなものです。
1:HTMLページテンプレートを作成し、特別な文字列を使用して、このページで動的に表示する場所($ HTMLSTRSTR $など)を表す場所を表します。
2:プログラムでは、このHTMLページを使用して、STRなどの文字列変数を読み取ります。
3:文字列の置換方法を使用して、特殊文字を最初のステップで必要なものに置き換えます。
4save;
わかりました、とても簡単ですが、私は今日C#でこのようなクラスを書き、動的に生成されるHTMLページを処理しました。私はまだそれを完全に書いていると思った。 .NETと接触したばかりなので、アドバイスをしたいと思っています。
注:このカテゴリのコードはすべてオリジナルではなく、一部のコードはネチズンのコードを参照しています!
以下は、変換クラスのコードです
コード
1つのシステム。
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 <key = "errlogpath" value = "aspxtohtml_log.txt"> </add>
15 </appsettings>*/
16/** /// </summary>
17パブリッククラスASPXTOHTML
18 {
19/** /// <summary>
20 ///テンプレートファイルで置き換えるパラメーターの数
21 /// </summary>
22プライベートint _templateparamcount = 0;
23/** /// <summary>
24 ///テンプレートファイルが配置されているパス
25 /// </summary>
26 Private String _TemplateFilePath = ConfigurationSettings.AppSettings ["TemplateFilepath"];
27/** /// <summary>
28 ///変換されたHTMLファイルに保存されているパス
29 /// </summary>
30プライベート文字列_htmlfilepath = configurationSettings.appsettings ["htmlfilepath"];
31
32/** /// <summary>
33 ///テンプレートページエンコーディング
34 /// </summary>
35プライベートエンコーディング_templatehtmlcode = encoding.getEncoding( "gb2312");
36
37/** /// <summary>
38 ///変換されたファイルエンコード
39 /// </summary>
40プライベートエンコーディング_code = encoding.getEncoding( "gb2312");
41
42/** /// <summary>
43 ///変換されたHTMLファイル名
44 /// </summary>
45プライベート文字列_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属性#リージョン属性
59
60/** /// <summary>
61 ///テンプレートファイルで置き換えるパラメーターの数
62 /// </summary>
63 Public Int TemplateParamCount
64 {
65 get
66 {
67 this._templateparamcountを返します。
68}
69セット//パラメーターの数を割り当てるとき、実際の配列は、テンプレートファイルのパラメーターとASPXファイルの実際の値に割り当てられ、HTMLファイルの実際の値を置き換えます。
70 {
71 if(value <0)
72新しい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他の
83 this._templateparamcount = 0;
84}
85}
86
87/** /// <summary>
88 ///テンプレートファイルが配置されているパス
89 ///
90 /// </summary>
91 Public String TemplateFilepath
92 {
93 get {this._templatefilepath;}
94 set {this._templatefilepath = value;}
95}
96/** /// <summary>
97 ///変換されたHTMLファイルに保存されているパス
98 /// </summary>
99パブリックストリングhtmlfilepath
100 {
101 get {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パブリックエンコーディングコード
117 {
118 get {return this._code;}
119 set {this._code = encoding.getEncoding(value.toString());}
120}
121/** /// <summary>
122 ///エラーファイルのパスが配置されています
123 /// </summary>
124パブリックストリングerrlogpath
125 {
126 get {
127 if(!(this._errlogpath == null)))
128 this._errlogpathを返します。
129 else
130 return "aspxtohtml_log.txt";
131}
132 set {this._errlogpath = value;}
133}
134
135
136 #endregion
137
138操作#地域操作
139
140/** /// <summary>
141 ///変換されたHTMLファイルが配置されている相対ファイルパスを取得します
142 ///たとえば:htmlfilepath = "/news/"の場合
143 ///変換されたHTMLファイル名は2005050505.htmlです
144 ///その後、返された値は/news/200505050505.htmlです
145 /// </summary>
146 /// <Remarks>このプロパティがStartConvertメソッドが呼び出される前に呼び出される場合はnullを返します</remarks>
147パブリックストリングhtmlfilevirtualpath
148 {
149 get
150 {
151 if(!(this._convertedfileName == "")))
152 return this.htmlfilepath+this._ConvertedFileName;
153他のもの
154返品null;
155}
156}
157
158/** /// <summary>
159 /// HTMLページパラメーター配列の支払い値
160 /// </summary>
161 /// <param name = "param"> </param>
162 public void setemplatefileparameter(string [] param)
163 {
164試してみてください
165 {
166 if(param.length == this.templateparamcount)
167 this._templatefileparameter = param;
168 // else //番号は元の定義とは異なります
169 //
170}
171キャッチ(system.exception ex)
172 {
173 writeerrfile(ex);
174}
175}
176/** /// <summary>
177 /// HTMLファイルに置き換えるASPXファイルのパラメーター配列の値を支払います
178 /// </summary>
179 /// <param name = "param"> </param>
180 public void setapxfileparameter(string [] param)
181 {
182試してみてください
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 /// <リターン> return値は作成の成功後のファイル名</returns>
198 /// <Remarks>このメソッドを呼び出す前に、setemplatefileparameterとsetaspxfileparameterメソッドが、対応する価値給操作</remarks>に呼び出されていることを確認する必要があります</remarks>
199 public string startconvert()
200 {
201 if(this._templatefileparameter.length == this._aspxfileparameter.length)
202 {
203 return writefile();
204}
205 else {
206返品null;
207}
208}
209/** /// <summary>
210 /// ASPXTOHTML変換を開始します
211 /// </summary>
212 /// <param name = "htmlparam"> HTMLテンプレートページのすべてのパラメーターの配列</param>
213 /// <param name = "aspxparam"> ASPXページは、HTMLテンプレートページのパラメーター値配列を置き換える必要があります</param>
214 /// <Returns> return値は、成功した作成後のファイル名</returns>
215 public string startconvert(string [] htmlparam、string [] aspxparam)
216 {
217 //最初にSettemplateFileParameterとSetAspxFileParameterメソッドを呼び出して、価値支払い操作を実行する
218 SetemplateFileParameter(htmlparam);
219 setaspxfileparameter(aspxparam);
220 //
221文字列fn = this.startconvert();
222 //
223 _CONVERTEDFILENAME = fn;
224 //
225 return fn;
226}
227
228/** /// <summary>
229 ///時間を使用して乱数を追加してファイル名を生成する
230 /// </summary>
231 /// <returns> </returns>
232プライベート文字列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 + MONSESTR + DAYSTR + HOURSTR + MINUTESTRESTR + SECONDSTR + MILLIONSECONDSTR + STRING.FORMAT( "{0:0000#}"、rd.next(100)) + "。html";
245 // DateTime.Now.ToString( "yyyymmddhhmmss")+"。html";
246}
247/** /// <summary>
248 ///変換
249 /// </summary>
250 /// <turnss> time </returns>によって名前が付けられたファイル名を返します
251プライベート文字列writefile()
252 {
253
254 //テンプレートファイルを読み取ります
255 string temp = httpcontext.current.server.mappath(this.templatefilepath);
256 StreamReader sr = null;
257 string str = "";
258試してみてください
259 {
260 sr = new StreamReader(Temp、this.templatehtmlcode);
261 str = sr.readtoend(); //ファイルを読み取ります
262}
263キャッチ(例外Ex)
264 {
265 //httpcontext.current.response.write(exp.message);
266 //httpcontext.current.response.end();
267 writeerrfile(ex);
268}
269最後に
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プライベートストリングsavefile(string str)
290 {
291 //ファイルを書きます
292 StreamWriter SW = null;
293試してみてください
294 {
295
296 String PATH = HTTPCONTEXT.CURRENT.SERVER.MAPPATH(this.htmlfilepath);
297 // HTMLファイル名
298文字列htmlfileName = getFileName();
299 sw = new StreamWriter(Path + HtmlfileName、false、this.code);
300 sw.write(str);
301 sw.flush();
302 return htmlfilename;
303}
304キャッチ(例外Ex)
305 {
306 writeerrfile(ex);
307}
308最後に
309 {
310 sw.close();
311}
312 return "";
313}
314
315/** /// <summary>
316 /// URLに渡して、HTMLコードをWebページに返す
317 /// </summary>
318 /// <param name = "url"> url </param>
319 /// <returns> </returns>
320パブリックストリングgeturltohtml(string url)
321 {
322試してみてください
323 {
324 System.net.WebRequest wreq = System.net.webrequest.create(url);
325 System.net.WebreaSponse 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キャッチ(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 setemplateParamcount(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 SetemplateParamCount(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 SetemplateParamCount(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 privatevoid writeerfile(例外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