Asp.Net如何生成靜態頁(轉載自多處)

方案1:
  1. /// <summary>
  2. /// 傳入URL返回網頁的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. catch (System.Exception ex)
  26. {
  27. errorMsg = ex.Message ;
  28. }
  29. return  "" ;
  30. }
你可以用這個函數獲取網頁的客戶端的html代碼,然後保存到.html文件裏就可以了。

方案2:
生成單個的靜態頁面不是難點,難的是各個靜態頁面間的關聯和鏈接如何保持完整;
特別是在頁面頻繁更新、修改、或刪除的情況下;

像阿里巴巴的頁面也全部是html的,估計用的是地址映射的功能
關於地址映射可參考:http://www.easewe.com/Article/ShowArticle.aspx?article=131

可以看看這個頁面,分析一下他的“競價倒計時”功能
http://info.china.alibaba.com/news/subject/v1-s5011580.html?head=top4&Bidding=home5

ASP.Net生成靜態HTML頁
在Asp中實現的生成靜態頁用到的FileSystemObject對象!
在.Net中涉及此類操作的是System.IO
以下是程序代碼 注:此代碼非原創!參考別人代碼

CODE:
  1. //生成HTML頁
  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. string  htmlfilename=DateTime.Now.ToString("yyyyMMddHHmmss" )+".html" ;
  23. // 替換內容
  24. // 這時,模板文件已經讀入到名稱爲str的變量中了
  25. str =str.Replace("ShowArticle" ,strText); //模板頁中的ShowArticle
  26. str = str.Replace("biaoti" ,strText);
  27. str = str.Replace("content" ,strContent);
  28. str = str.Replace("author" ,strAuthor);
  29. // 寫文件
  30. try
  31. {
  32. sw = new  StreamWriter(path + htmlfilename , false , code);
  33. sw.Write(str);
  34. sw.Flush();
  35. }
  36. catch (Exception ex)
  37. {
  38. HttpContext.Current.Response.Write(ex.Message);
  39. HttpContext.Current.Response.End();
  40. }
  41. finally
  42. {
  43. sw.Close();
  44. }
  45. return  true ;
此函數放在Conn.CS基類中了
在添加新聞的代碼中引用 注:工程名爲Hover

if(Hover.Conn.WriteFilethis.Title.Text.ToString),this.Content.Text.ToString),this.Author.Text.ToString)))
{
Response.Write("添加成功");
}
else
{
Response.Write("生成HTML出錯!");
}


模板頁Text.html代碼

CODE:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
<HEAD>
<title>ShowArticle</title>
<body>
biaoti
<br>
content<br>
author
</body>
</HTML>
biaoti
<br>
content<br>
author
</body>
</HTML>

提示添加成功後會出以當前時間爲文件名的html文件!上面只是把傳遞過來的幾個參數直接寫入了HTML文件中,在實際應用中需要先添加數據庫,然後再寫入HTML文件

方案3:
給一個客戶端參考的例子(SJ)
它的作用在於以客戶端的方式獲取某個頁面的代碼,然後可以做爲其他用途,本例是直接輸出
  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("您的機器不支持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>
方案4:學csdn一樣。用xml保存數據,模版XSL也只有一個文件。
使用xml來保存數據,使用xsl來定義模板並且生稱數據。可以通過xsl來很方便的在客戶端或者服務段顯示數據。如果要生成靜態葉面那更簡單了。去查一下.net的xml類包問題解決。
優點:可以方便快速轉換成你想要的格式和內容。
缺點:需要學習更多的內容,不好入門。

方案5:
思路

  1. 利用如Dw-Mx這樣的工具生成html格式的模板,在需要添加格式的地方加入特殊標記(如$htmlformat$),動態生成文件時利用代碼讀取此模板,然後獲得前臺輸入的內容,添加到此模板的標記位置中,生成新文件名後寫入磁盤,寫入後再向數據庫中寫入相關數據。
2. 使用後臺代碼硬編碼Html文件,可以使用HtmlTextWriter類來寫html文件。
優點

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

  2. 靜態html文件利用MS Windows2000的Index Server可以建立全文搜索引擎,利用asp.net可以以DataTable的方式得到搜索結果。而Win2000的Index服務無法查找xml文件的內容。如果包括了數據庫搜索與Index索引雙重查找,那麼此搜索功能將非常強大。

  3. 節省服務器的負荷,請求一個靜態的html文件比一個aspx文件服務器資源節省許多。

  缺點

  思路二:如果用硬編碼的方式,工作量非常大,需要非常多的html代碼。調試困難。而且使用硬編碼生成的html樣式無法修改,如果網站更換樣式,那麼必須得重新編碼,給後期帶來巨大的工作量。

  因此這裏採用的是第一種思路

  示列代碼

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

  1.  <html>
  2.   <head>
  3.   <title></title>
  4.   <meta http-equiv ="Content-Type"  content ="text/html; charset=gb2312"
  5.   </head>
  6.   <body >
  7.   <table $htmlformat[0] height ="100%"  border ="0"  width ="100%"  cellpadding ="10"  cellspacing ="0"  bgcolor ="#eeeeee"  style ="border:1px solid #000000"
  8.   <tr>
  9.   <td width ="100%"  valign ="middle"  align ="left"
  10.   <span style ="color: $htmlformat[1];font-size: $htmlformat[2]" >$htmlformat[3]</span>
  11.   </td>
  12.   </tr>
  13.   </table>
  14.   </body>
  15.   </html>
2.asp.net代碼:

  1. //---------------------讀html模板頁面到stringbuilder對象裏----
  2.   string [] format=new  string [4];//定義和htmlyem標記數目一致的數組
  3.   StringBuilder htmltext=new  StringBuilder();
  4.   try
  5.   {
  6.    using  (StreamReader sr = new  StreamReader("存放模板頁面的路徑和頁面名" ))
  7.    {
  8.   String line;
  9.   while  ((line = sr.ReadLine()) != null )
  10.   {
  11.    htmltext.Append(line);
  12.   }
  13.   sr.Close();
  14.    }
  15.   }
  16.   catch
  17.   {
  18.    Response.Write("<Script>alert('讀取文件錯誤')</Script>" );
  19.   }
  20.   //---------------------給標記數組賦值------------
  21.   format[0]="background=" bg.jpg"" ;//背景圖片
  22.   format[1]= "#990099" ;//字體顏色
  23.   format[2]="150px" ;//字體大小
  24.   format[3]= "<marquee>生成的模板html頁面</marquee>" ;//文字說明
  25.   //----------替換htm裏的標記爲你想加的內容
  26.   for (int  i=0;i<4;i++)
  27.   {
  28.    htmltext.Replace("$htmlformat[" +i+"]" ,format[i]);
  29.   }
  30.   //----------生成htm文件------------------――
  31.   try
  32.   {
  33.    using (StreamWriter sw=new  StreamWriter("存放路徑和頁面名" ,false ,System.Text.Encoding.GetEncoding("GB2312" )))
  34.   {
  35.    sw.WriteLine(htmltext);
  36.    sw.Flush();
  37.    sw.Close();
  38.   }
  39.   }
  40.   catch
  41.   {
  42.   Response.Write ("The file could not be wirte:" );
  43.   }
小結
  用此方法可以方便的生成html文件。程序使用了是循環替換,因此對需替換大量元素的模板速度非常快。
 
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章