自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

使用ASP.NET模板生成HTML靜態(tài)頁(yè)面的五種方案

開(kāi)發(fā) 后端
在Asp中實(shí)現(xiàn)的生成靜態(tài)頁(yè)用到的FileSystemObject對(duì)象,在.Net中涉及此類操作的是System.IO。本文介紹了使用ASP.NET模板生成HTML靜態(tài)頁(yè)面的五種方案。

使用ASP.NET模版生成HTML靜態(tài)頁(yè)面并不是難事,主要是使各個(gè)靜態(tài)頁(yè)面間的關(guān)聯(lián)和鏈接如何保持完整。本文介紹了使用ASP.NET模版生成HTML靜態(tài)頁(yè)面的五種方案。

ASP.NET模版生成HTML靜態(tài)頁(yè)面方案1:

  1. /// < summary>   
  2. /// 傳入U(xiǎn)RL返回網(wǎng)頁(yè)的html代碼   
  3. /// < /summary>   
  4. /// < param name="Url">URL< /param>   
  5. /// < returns>< /returns>   
  6. public static string getUrltoHtml(string Url)   
  7. {   
  8. errorMsg = "";   
  9. try   
  10. {   
  11. System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url);   
  12. // Get the response instance.   
  13. System.Net.WebResponse wResp =wReq.GetResponse();   
  14. // Read an HTTP-specific property   
  15. //if (wResp.GetType() ==HttpWebResponse)   
  16. //{   
  17. //DateTime updated =((System.Net.HttpWebResponse)wResp).LastModified;   
  18. //}   
  19. // Get the response stream.   
  20. System.IO.Stream respStream = wResp.GetResponseStream();   
  21. // Dim reader As StreamReader = New StreamReader(respStream)   
  22. System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding("gb2312"));   
  23. return reader.ReadToEnd();   
  24.  
  25. }   
  26. catch(System.Exception ex)   
  27. {   
  28. errorMsg = ex.Message ;   
  29. }   
  30. return "";   
  31. }   
  32.  

你可以用這個(gè)函數(shù)獲取網(wǎng)頁(yè)的客戶端的html代碼,然后保存到.html文件里就可以了。

ASP.NET模版生成HTML靜態(tài)頁(yè)面方案2:

生成單個(gè)的靜態(tài)頁(yè)面不是難點(diǎn),難的是各個(gè)靜態(tài)頁(yè)面間的關(guān)聯(lián)和鏈接如何保持完整;

特別是在頁(yè)面頻繁更新、修改、或刪除的情況下;

像阿里巴巴的頁(yè)面也全部是html的,估計(jì)用的是地址映射的功能

可以看看這個(gè)頁(yè)面,分析一下他的“競(jìng)價(jià)倒計(jì)時(shí)”功能

http://info.china.alibaba.com/news/subject/v1-s5011580.html?head=top4&Bidding=home5

ASP.Net生成靜態(tài)HTML頁(yè)

在Asp中實(shí)現(xiàn)的生成靜態(tài)頁(yè)用到的FileSystemObject對(duì)象!

在.Net中涉及此類操作的是System.IO

以下是程序代碼 注:此代碼非原創(chuàng)!參考別人代碼

  1. //生成HTML頁(yè)   
  2. public static bool WriteFile(string strText,string strContent,string strAuthor)   
  3. {   
  4. string path = HttpContext.Current.Server.MapPath("/news/");   
  5. Encoding code = Encoding.GetEncoding("gb2312");   
  6. // 讀取模板文件   
  7. string temp = HttpContext.Current.Server.MapPath("/news/text.html");   
  8. StreamReader sr=null;   
  9. StreamWriter sw=null;   
  10. string str="";   
  11. try   
  12. {   
  13. sr = new StreamReader(temp, code);   
  14. str = sr.ReadToEnd(); // 讀取文件   
  15. }   
  16. catch(Exception exp)   
  17. {   
  18. HttpContext.Current.Response.Write(exp.Message);   
  19. HttpContext.Current.Response.End();   
  20. sr.Close();   
  21. }   
  22.  
  23.  
  24. string htmlfilename=DateTime.Now.ToString("yyyyMMddHHmmss")+".html";   
  25. // 替換內(nèi)容   
  26. // 這時(shí),模板文件已經(jīng)讀入到名稱為str的變量中了   
  27. str =str.Replace("ShowArticle",strText); //模板頁(yè)中的ShowArticle   
  28. str = str.Replace("biaoti",strText);   
  29. str = str.Replace("content",strContent);   
  30. str = str.Replace("author",strAuthor);   
  31. // 寫(xiě)文件   
  32. try   
  33. {   
  34. sw = new StreamWriter(path + htmlfilename , false, code);   
  35. sw.Write(str);   
  36. sw.Flush();   
  37. }   
  38. catch(Exception ex)   
  39. {   
  40. HttpContext.Current.Response.Write(ex.Message);   
  41. HttpContext.Current.Response.End();   
  42. }   
  43. finally   
  44. {   
  45. sw.Close();   
  46. }   
  47. return true;   
  48.  

此函數(shù)放在Conn.CS基類中了

在添加新聞的代碼中引用 注:工程名為Hover

  1. if(Hover.Conn.WriteFilethis.Title.Text.ToString),this.Content.Text.ToString),this.Author.Text.ToString)))   
  2. {   
  3. Response.Write("添加成功");   
  4. }   
  5. else   
  6. {   
  7. Response.Write("生成HTML出錯(cuò)!");   
  8. }   
  9.  

模板頁(yè)Text.html代碼

  1. < !DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >   
  2. < HTML>   
  3. < HEAD>   
  4. < title>ShowArticle< /title>   
  5. < body>   
  6. biaoti   
  7. < br>   
  8. content< br>   
  9. author   
  10. < /body>   
  11. < /HTML>   
  12. biaoti   
  13. < br>   
  14. content< br>   
  15. author   
  16. < /body>   
  17. < /HTML>   
  18.  

提示添加成功后會(huì)出以當(dāng)前時(shí)間為文件名的html文件!上面只是把傳遞過(guò)來(lái)的幾個(gè)參數(shù)直接寫(xiě)入了HTML文件中,在實(shí)際應(yīng)用中需要先添加數(shù)據(jù)庫(kù),然后再寫(xiě)入HTML文件

ASP.NET模版生成HTML靜態(tài)頁(yè)面方案3:

給一個(gè)客戶端參考的例子(JS)

它的作用在于以客戶端的方式獲取某個(gè)頁(yè)面的代碼,然后可以做為其他用途,本例是直接輸出

  1. < script>   
  2. var oXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");   
  3. oXmlHttp.open("GET","http://www.163.com"false);   
  4. oXmlHttp.send()   
  5. var oStream = new ActiveXObject("ADODB.Stream");   
  6. if(oStream == null)   
  7. alert("您的機(jī)器不支持ADODB.Stream.")   
  8. else   
  9. {   
  10. oStream.Type=1;   
  11. oStream.Mode=3;   
  12. oStream.Open() ;   
  13. oStream.Write(oXmlHttp.responseBody);   
  14. oStream.Position= 0;   
  15. oStream.Type= 2;   
  16. oStream.Charset="gb2312";   
  17. var result= oStream.ReadText();   
  18. oStream.Close();   
  19. oStream = null;   
  20. var aa = window.open("","")   
  21. document.write(result);   
  22. aa.document.write(result);   
  23. }   
  24. < /script>   

ASP.NET模版生成HTML靜態(tài)頁(yè)面方案4:

學(xué)csdn一樣。用xml保存數(shù)據(jù),模版XSL也只有一個(gè)文件。

使用xml來(lái)保存數(shù)據(jù),使用xsl來(lái)定義模板并且生稱數(shù)據(jù)??梢酝ㄟ^(guò)xsl來(lái)很方便的在客戶端或者服務(wù)段顯示數(shù)據(jù)。如果要生成靜態(tài)葉面那更簡(jiǎn)單了。去查一下.net的xml類包問(wèn)題解決。

優(yōu)點(diǎn):可以方便快速轉(zhuǎn)換成你想要的格式和內(nèi)容。

缺點(diǎn):需要學(xué)習(xí)更多的內(nèi)容,不好入門(mén)。

ASP.NET模版生成HTML靜態(tài)頁(yè)面方案5:

思路

1. 利用如Dw-Mx這樣的工具生成html格式的模板,在需要添加格式的地方加入特殊標(biāo)記(如$htmlformat$),動(dòng)態(tài)生成文件時(shí)利用代碼讀取此模板,然后獲得前臺(tái)輸入的內(nèi)容,添加到此模板的標(biāo)記位置中,生成新文件名后寫(xiě)入磁盤(pán),寫(xiě)入后再向數(shù)據(jù)庫(kù)中寫(xiě)入相關(guān)數(shù)據(jù)。

2. 使用后臺(tái)代碼硬編碼Html文件,可以使用HtmlTextWriter類來(lái)寫(xiě)html文件。

優(yōu)點(diǎn)

1. 可以建立非常復(fù)雜的頁(yè)面,利用包含js文件的方法,在js文件內(nèi)加入document.write()方法可以在所有頁(yè)面內(nèi)加入如頁(yè)面頭,廣告等內(nèi)容。

2. 靜態(tài)html文件利用MS Windows2000的Index Server可以建立全文搜索引擎,利用asp.net可以以DataTable的方式得到搜索結(jié)果。而Win2000的Index服務(wù)無(wú)法查找xml文件的內(nèi)容。如果包括了數(shù)據(jù)庫(kù)搜索與Index索引雙重查找,那么此搜索功能將非常強(qiáng)大。

3. 節(jié)省服務(wù)器的負(fù)荷,請(qǐng)求一個(gè)靜態(tài)的html文件比一個(gè)aspx文件服務(wù)器資源節(jié)省許多。

缺點(diǎn)

思路二: 如果用硬編碼的方式,工作量非常大,需要非常多的html代碼。調(diào)試?yán)щy。而且使用硬編碼生成的html樣式無(wú)法修改,如果網(wǎng)站更換樣式,那么必須得重新編碼,給后期帶來(lái)巨大的工作量。

因此這里采用的是***種思路

示列代碼

1.定義(template.htm)html模板頁(yè)面

  1. <html>   
  2.  
  3. <head>   
  4.  
  5. <title></title>   
  6.  
  7. <meta http-equiv="Content-Type" content="text/html; charset=gb2312">   
  8.  
  9. </head>   
  10.  
  11. <body >   
  12.  
  13. <table $htmlformat[0] height="100%" border="0" width="100%" cellpadding="10" cellspacing="0" bgcolor="#eeeeee" style="border:1px solid #000000">   
  14.  
  15. <tr>   
  16.  
  17. <td width="100%" valign="middle" align="left">   
  18.  
  19. <span style="color: $htmlformat[1];font-size: $htmlformat[2]">$htmlformat[3]</span>   
  20.  
  21. </td>   
  22.  
  23. </tr>   
  24.  
  25. </table>   
  26.  
  27. </body>   
  28.  
  29. </html>   
  30.  

2.asp.net代碼:

  1. //---------------------讀html模板頁(yè)面到stringbuilder對(duì)象里----   
  2.  
  3. string[] format=new string[4];//定義和htmlyem標(biāo)記數(shù)目一致的數(shù)組   
  4.  
  5. StringBuilder htmltext=new StringBuilder();   
  6.  
  7. try   
  8.  
  9. {   
  10.  
  11. using (StreamReader sr = new StreamReader("存放模板頁(yè)面的路徑和頁(yè)面名"))   
  12.  
  13. {   
  14.  
  15. String line;   
  16.  
  17. while ((line = sr.ReadLine()) != null)   
  18.  
  19. {   
  20.  
  21. htmltext.Append(line);   
  22.  
  23. }   
  24.  
  25. sr.Close();   
  26.  
  27. }   
  28.  
  29. }   
  30.  
  31. catch   
  32.  
  33. {   
  34.  
  35. Response.Write("<Script>alert('讀取文件錯(cuò)誤')</Script>");   
  36.  
  37. }   
  38.  
  39. //---------------------給標(biāo)記數(shù)組賦值------------   
  40.  
  41. format[0]="background="bg.jpg"";//背景圖片   
  42.  
  43. format[1]= "#990099";//字體顏色   
  44.  
  45. format[2]="150px";//字體大小   
  46.  
  47. format[3]= "<marquee>生成的模板html頁(yè)面</marquee>";//文字說(shuō)明   
  48.  
  49. //----------替換htm里的標(biāo)記為你想加的內(nèi)容   
  50.  
  51. for(int i=0;i<4;i++)   
  52.  
  53. {   
  54.  
  55. htmltext.Replace("$htmlformat["+i+"]",format[i]);   
  56.  
  57. }   
  58.  
  59. //----------生成htm文件------------------――   
  60.  
  61. try   
  62.  
  63. {   
  64.  
  65. using(StreamWriter sw=new StreamWriter("存放路徑和頁(yè)面名",false,System.Text.Encoding.GetEncoding("GB2312")))   
  66.  
  67. {   
  68.  
  69. sw.WriteLine(htmltext);   
  70.  
  71. sw.Flush();   
  72.  
  73. sw.Close();   
  74.  
  75. }   
  76.  
  77. }   
  78.  
  79. catch   
  80.  
  81. {   
  82.  
  83. Response.Write ("The file could not be wirte:");   
  84.  
  85. }   
  86.  

小結(jié)

用此方法可以方便的生成html文件。程序使用了是循環(huán)替換,因此對(duì)需替換大量元素的模板速度非常快。

【編輯推薦】

  1. 什么是ASP.NET DataGrid控件:完全模板化的網(wǎng)格
  2. ASP.NET的GridView與DataGrid控件比較淺析
  3. .NET新手指南:輕松自定義DataGridView控件
  4. C#中對(duì)DatagridView的部分常用操作
  5. ASP.net中將DataGrid的內(nèi)容導(dǎo)出為excel文件
責(zé)任編輯:yangsai 來(lái)源: 百度空間
相關(guān)推薦

2009-07-23 14:21:55

ASP.NET頁(yè)面

2009-07-31 13:06:53

CheckBoxLisASP.NET頁(yè)面

2009-08-14 13:37:25

ASP.NET靜態(tài)頁(yè)面

2009-07-24 10:53:51

ASP.NET實(shí)現(xiàn)靜態(tài)

2009-08-05 14:17:27

ASP.NET錯(cuò)誤頁(yè)面

2009-10-19 15:14:48

aspx擴(kuò)展

2009-07-23 11:11:41

2009-07-29 17:26:39

ASP.NET頁(yè)面

2009-07-23 14:17:41

2012-05-04 15:54:16

ASP.NET

2009-07-20 16:10:31

ASP.NET頁(yè)面靜態(tài)

2009-07-31 10:23:44

緩存頁(yè)面ASP.NET緩存

2009-10-09 10:52:43

ASP.NET模板引擎

2009-08-03 13:38:18

ASP.NET編程模型

2009-08-07 15:24:16

ASP.NET模板控件

2009-07-29 14:35:34

頁(yè)面輸出緩存ASP.NET

2009-07-23 10:52:38

2009-07-27 15:25:40

aspx頁(yè)面ASP.NET

2009-07-31 10:33:54

ASP.NET頁(yè)面輸出

2009-08-05 18:22:55

點(diǎn)贊
收藏

51CTO技術(shù)棧公眾號(hào)