99亚洲_成人性视频免费网站_av在线播放网址_免费成人在线网站_亚洲综合婷婷_亚洲一区二区国产

訂閱本欄目 RSS您所在的位置: 深山工作室 > asp.net > 正文

根據頁面模板動態生成html頁面

博客網 2008/1/7 21:31:06 admin 字體: 瀏覽 31700

一直以為動態生成靜態頁面不好做,昨天在網上找了下,其實很簡單,思路大概是這樣的,
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 /**////
9 /// AspxToHtml 的摘要說明。
10 /// 注:使用此類,你可以在web.config文件對模板類進行配置.如下
11 /**//*
12
13
14
15 */
16 /**////
17 public class AspxToHtml
18 {
19 /**////
20 /// 模板文件中要替代的參數個數
21 ///
22 private int _templateParamCount=0;
23 /**////
24 /// 模板文件所在的路徑
25 ///
26 private string _templateFilePath
=ConfigurationSettings.AppSettings["templateFilePath"];
27 /**////
28 /// 轉換后的html文件所存放的路徑
29 ///
30 private string _htmlFilePath
=ConfigurationSettings.AppSettings["htmlFilePath"];
31
32 /**////
33 /// 模板頁頁面編碼
34 ///
35 private Encoding _templateHtmlCode
=Encoding.GetEncoding("gb2312");
36
37 /**////
38 /// 轉換后的文件編碼
39 ///
40 private Encoding _code = Encoding.GetEncoding("gb2312");
41
42 /**////
43 /// 轉換后的html文件名
44 ///
45 private string _convertedFilename="";
46 /**////
47 /// 模板文件中的參數
48 ///
49 private string[] _templateFileparameter ;
50
51 /**////
52 /// aspx文件中的要代替HTML文件中的參數實際值
53 ///
54 private string[] _aspxFileparameter;
55
56 private string _errlogPath = ConfigurationSettings.AppSettings["ErrLogPath"];
57
58 屬性#region 屬性
59
60 /**////
61 /// 模板文件中要替代的參數個數
62 ///
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 /**////
88 /// 模板文件所在的路徑
89 ///
90 ///
91 public string TemplateFilePath
92 {
93 get{ return this._templateFilePath;}
94 set{ this._templateFilePath=value;}
95 }
96 /**////
97 /// 轉換后的html文件所存放的路徑
98 ///
99 public string HtmlFilePath
100 {
101 get{ return this._htmlFilePath;}
102 set{ this._htmlFilePath=value;}
103 }
104
105 /**////
106 /// html模板文件編碼
107 ///
108 public Encoding TemplateHtmlCode
109 {
110 get{ return this._templateHtmlCode;}
111 set{ this._templateHtmlCode=Encoding.GetEncoding(value.ToString());}
112 }
113 /**////
114 /// 編碼
115 ///
116 public Encoding Code
117 {
118 get{ return this._code;}
119 set{ this._code=Encoding.GetEncoding(value.ToString());}
120 }
121 /**////
122 /// 錯誤文件所在路徑
123 ///
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 /**////
141 /// 獲取轉換后的html文件所在相對文件路徑
142 /// 如:如果HtmlFilePath="/news/"
143 /// 轉換后的html文件名為200505050505.html
144 /// 則返回的值為/news/200505050505.html
145 ///
146 /// 如果在未調用StartConvert方法之前調用此屬性則返回
null
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 /**////
159 /// 為HTML頁面參數數組付值
160 ///
161 ///
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 /**////
177 /// 為aspx文件中將要替換html文件中的參數數組付值
178 ///
179 ///
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 /**////
195 /// 開始進行aspxTohtml轉換
196 ///
197 /// 返回值為成功創建后的文件名稱
198 /// 在調用此方法之前必需確定已調用setTemplateFileparameter 和
setAspxFileparameter方法進行相應的付值操作
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 /**////
210 /// 開始進行aspxTohtml轉換
211 ///
212 /// html模板頁中的所有參數數組
213 /// aspx頁面中要代替html模板頁中參數值數組

214 /// 返回值為成功創建后的文件名稱
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 /**////
229 /// 用時間加隨機數生成一個文件名
230 ///
231 ///
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 /**////
248 /// 進行轉換處理
249 ///
250 /// 返回以時間命名的文件名
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;i276 {
277 str =str.Replace(this._templateFileparameter[i],this._aspxFileparameter[i]);
278 }
279
280 return savefile(str);
281 }
282
283 /**////
284 ///
285 ///
286 ///
287 ///
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 /**////
316 /// 傳入URL返回網頁的html代碼
317 ///
318 /// URL
319 ///
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 /**////
355 /// 提供欲代替的參數個數
356 ///
357 ///
358 public AspxToHtml(int templateParamCount)
359 {
360 settemplateParamCount(templateParamCount);
361
362 }
363 /**////
364 ///
365 ///
366 /// html模板頁中的參數個數
367 /// 生成的html文件所存放的文件夾路徑
368 /// html模板頁路徑
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 /**////
381 /// 把錯誤寫入文件方法#region 把錯誤寫入文件方法
382 ///
383 ///
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

相關閱讀
初學入門:一組常用的彈出窗口用法總結
利用javascript高亮關鍵詞系列
留言板留言板V7.22
sql server中前綴為PK、UK、DF、CK、FK表的意思
asp利用正則檢查手機端的瀏覽器標識來確定是否是手機訪問 如果是手機訪問就使用手機模板
uni-app瀏覽歷史記錄功能實現
網頁制作中關于瀏覽器寬度和高度的設定研究(轉載)
純div+css制作的彈出菜單-04
共有0條關于《根據頁面模板動態生成html頁面》的評論
發表評論
正在加載評論......
返回頂部發表評論
呢 稱:
表 情:
內 容:
評論內容:不能超過 1000 字,需審核,請自覺遵守互聯網相關政策法規。
驗證碼: 驗證碼 
網友評論聲明,請自覺遵守互聯網相關政策法規。

您發布的評論即表示同意遵守以下條款:
一、不得利用本站危害國家安全、泄露國家秘密,不得侵犯國家、社會、集體和公民的合法權益;
二、不得發布國家法律、法規明令禁止的內容;互相尊重,對自己在本站的言論和行為負責;
三、本站對您所發布內容擁有處置權。

更多信息>>欄目類別選擇
百度小程序開發
微信小程序開發
微信公眾號開發
uni-app
asp函數庫
ASP
DIV+CSS
HTML
python
更多>>同類信息
ASP.NET中Cookie編程的基礎知識
ASP.NET 2.0跨網頁提交的三法
ASP.NET 2和IIS 7.0發生不兼容變化
ASP.NET中文亂碼問題的解決。
ASP.Net 2.0中的5個數據控件
根據頁面模板動態生成html頁面
更多>>最新添加文章
dw里面查找替換使用正則刪除sqlserver里面的CONSTRAINT
Android移動端自動化測試:使用UIAutomatorViewer與Selenium定位元素
抖音直播音掛載小雪花 懂車帝小程序
javascript獲取瀏覽器指紋可以用來做投票
火狐Mozilla Firefox出現:無法載入您的Firefox配置文件 它可能已經丟失 或是無法訪問 問題解決集合處理辦法
在Android、iOS、Windows、MacOS中微信小程序的文件存放路徑
python通過代碼修改pip下載源讓下載庫飛起
python里面requests.post返回的res.text還有其它的嗎
更多>>隨機抽取信息
洪江市道德模范評選 - 網絡投票
針對table,div隔行變色的JS方法
深山行者asp購物網終于上線了
中國簽證行業領導者--91出國簽證網
做好的網站把http改成https申請SSL域名證書
對于網站改版的一些記憶。
主站蜘蛛池模板: 樱桃小丸子在线观看 | 欧美在线 | 亚洲 | 午夜小视频在线观看 | 毛片a级 | 欧美日韩第一区 | 日韩欧美视频免费 | 日本少妇bbbb爽爽bbb美 | 激情婷婷 | 久热久爱| 久久影院一区 | 亚洲欧美制服诱惑 | 日韩一级视频 | 欧美一级二级三级 | 欧美视频免费在线 | 韩日av在线免费观看 | 欧美日韩一区精品 | 久久中国| 国产免费一区 | 亚洲专区中文字幕 | 91视频免费观看 | 精品免费视频 | 亚洲国产激情 | 在线电影亚洲 | 激情国产视频 | 成年人在线免费观看视频网站 | 中文在线一区二区 | 一级毛片视频 | 日韩不卡在线 | 国产精品久久久久久久久久99 | 亚洲乱码日产精品一二三 | 榴莲视频成人在线观看 | 中文字幕日韩欧美 | 日韩亚洲| 国产一区二区三区在线视频观看 | 久久精品99国产精品日本 | 国产在线精品一区二区 | 精品久久久久久国产 | 国产一区二区三区久久久 | 欧美日韩欧美日韩 | 欧美在线观看免费观看视频 | 国产91精品亚洲精品日韩已满 |