使用ASP.NET模板生成HTML靜態(tài)頁面的五種方案
使用ASP.NET模版生成HTML靜態(tài)頁面并不是難事,主要是使各個靜態(tài)頁面間的關(guān)聯(lián)和鏈接如何保持完整。本文介紹了使用ASP.NET模版生成HTML靜態(tài)頁面的五種方案。
ASP.NET模版生成HTML靜態(tài)頁面方案1:
- /// < summary>
 - /// 傳入URL返回網(wǎng)頁的html代碼
 - /// < /summary>
 - /// < param name="Url">URL< /param>
 - /// < returns>< /returns>
 - public static string getUrltoHtml(string Url)
 - {
 - errorMsg = "";
 - try
 - {
 - System.Net.WebRequest wReq = System.Net.WebRequest.Create(Url);
 - // Get the response instance.
 - System.Net.WebResponse wResp =wReq.GetResponse();
 - // Read an HTTP-specific property
 - //if (wResp.GetType() ==HttpWebResponse)
 - //{
 - //DateTime updated =((System.Net.HttpWebResponse)wResp).LastModified;
 - //}
 - // Get the response stream.
 - System.IO.Stream respStream = wResp.GetResponseStream();
 - // Dim reader As StreamReader = New StreamReader(respStream)
 - System.IO.StreamReader reader = new System.IO.StreamReader(respStream, System.Text.Encoding.GetEncoding("gb2312"));
 - return reader.ReadToEnd();
 - }
 - catch(System.Exception ex)
 - {
 - errorMsg = ex.Message ;
 - }
 - return "";
 - }
 
你可以用這個函數(shù)獲取網(wǎng)頁的客戶端的html代碼,然后保存到.html文件里就可以了。
ASP.NET模版生成HTML靜態(tài)頁面方案2:
生成單個的靜態(tài)頁面不是難點,難的是各個靜態(tài)頁面間的關(guān)聯(lián)和鏈接如何保持完整;
特別是在頁面頻繁更新、修改、或刪除的情況下;
像阿里巴巴的頁面也全部是html的,估計用的是地址映射的功能
可以看看這個頁面,分析一下他的“競價倒計時”功能
http://info.china.alibaba.com/news/subject/v1-s5011580.html?head=top4&Bidding=home5
ASP.Net生成靜態(tài)HTML頁
在Asp中實現(xiàn)的生成靜態(tài)頁用到的FileSystemObject對象!
在.Net中涉及此類操作的是System.IO
以下是程序代碼 注:此代碼非原創(chuàng)!參考別人代碼
- //生成HTML頁
 - public static bool WriteFile(string strText,string strContent,string strAuthor)
 - {
 - string path = HttpContext.Current.Server.MapPath("/news/");
 - Encoding code = Encoding.GetEncoding("gb2312");
 - // 讀取模板文件
 - string temp = HttpContext.Current.Server.MapPath("/news/text.html");
 - StreamReader sr=null;
 - StreamWriter sw=null;
 - string str="";
 - try
 - {
 - sr = new StreamReader(temp, code);
 - str = sr.ReadToEnd(); // 讀取文件
 - }
 - catch(Exception exp)
 - {
 - HttpContext.Current.Response.Write(exp.Message);
 - HttpContext.Current.Response.End();
 - sr.Close();
 - }
 - string htmlfilename=DateTime.Now.ToString("yyyyMMddHHmmss")+".html";
 - // 替換內(nèi)容
 - // 這時,模板文件已經(jīng)讀入到名稱為str的變量中了
 - str =str.Replace("ShowArticle",strText); //模板頁中的ShowArticle
 - str = str.Replace("biaoti",strText);
 - str = str.Replace("content",strContent);
 - str = str.Replace("author",strAuthor);
 - // 寫文件
 - try
 - {
 - sw = new StreamWriter(path + htmlfilename , false, code);
 - sw.Write(str);
 - sw.Flush();
 - }
 - catch(Exception ex)
 - {
 - HttpContext.Current.Response.Write(ex.Message);
 - HttpContext.Current.Response.End();
 - }
 - finally
 - {
 - sw.Close();
 - }
 - return true;
 
此函數(shù)放在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代碼
- < !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>
 
提示添加成功后會出以當(dāng)前時間為文件名的html文件!上面只是把傳遞過來的幾個參數(shù)直接寫入了HTML文件中,在實際應(yīng)用中需要先添加數(shù)據(jù)庫,然后再寫入HTML文件
ASP.NET模版生成HTML靜態(tài)頁面方案3:
給一個客戶端參考的例子(JS)
它的作用在于以客戶端的方式獲取某個頁面的代碼,然后可以做為其他用途,本例是直接輸出
- < script>
 - var oXmlHttp = new ActiveXObject("Microsoft.XMLHTTP");
 - oXmlHttp.open("GET","http://www.163.com", false);
 - oXmlHttp.send()
 - var oStream = new ActiveXObject("ADODB.Stream");
 - if(oStream == null)
 - alert("您的機器不支持ADODB.Stream.")
 - else
 - {
 - oStream.Type=1;
 - oStream.Mode=3;
 - oStream.Open() ;
 - oStream.Write(oXmlHttp.responseBody);
 - oStream.Position= 0;
 - oStream.Type= 2;
 - oStream.Charset="gb2312";
 - var result= oStream.ReadText();
 - oStream.Close();
 - oStream = null;
 - var aa = window.open("","")
 - document.write(result);
 - aa.document.write(result);
 - }
 - < /script>
 
ASP.NET模版生成HTML靜態(tài)頁面方案4:
學(xué)csdn一樣。用xml保存數(shù)據(jù),模版XSL也只有一個文件。
使用xml來保存數(shù)據(jù),使用xsl來定義模板并且生稱數(shù)據(jù)。可以通過xsl來很方便的在客戶端或者服務(wù)段顯示數(shù)據(jù)。如果要生成靜態(tài)葉面那更簡單了。去查一下.net的xml類包問題解決。
優(yōu)點:可以方便快速轉(zhuǎn)換成你想要的格式和內(nèi)容。
缺點:需要學(xué)習(xí)更多的內(nèi)容,不好入門。
ASP.NET模版生成HTML靜態(tài)頁面方案5:
思路
1. 利用如Dw-Mx這樣的工具生成html格式的模板,在需要添加格式的地方加入特殊標(biāo)記(如$htmlformat$),動態(tài)生成文件時利用代碼讀取此模板,然后獲得前臺輸入的內(nèi)容,添加到此模板的標(biāo)記位置中,生成新文件名后寫入磁盤,寫入后再向數(shù)據(jù)庫中寫入相關(guān)數(shù)據(jù)。
2. 使用后臺代碼硬編碼Html文件,可以使用HtmlTextWriter類來寫html文件。
優(yōu)點
1. 可以建立非常復(fù)雜的頁面,利用包含js文件的方法,在js文件內(nèi)加入document.write()方法可以在所有頁面內(nèi)加入如頁面頭,廣告等內(nèi)容。
2. 靜態(tài)html文件利用MS Windows2000的Index Server可以建立全文搜索引擎,利用asp.net可以以DataTable的方式得到搜索結(jié)果。而Win2000的Index服務(wù)無法查找xml文件的內(nèi)容。如果包括了數(shù)據(jù)庫搜索與Index索引雙重查找,那么此搜索功能將非常強大。
3. 節(jié)省服務(wù)器的負(fù)荷,請求一個靜態(tài)的html文件比一個aspx文件服務(wù)器資源節(jié)省許多。
缺點
思路二: 如果用硬編碼的方式,工作量非常大,需要非常多的html代碼。調(diào)試?yán)щy。而且使用硬編碼生成的html樣式無法修改,如果網(wǎng)站更換樣式,那么必須得重新編碼,給后期帶來巨大的工作量。
因此這里采用的是***種思路
示列代碼
1.定義(template.htm)html模板頁面
- <html>
 - <head>
 - <title></title>
 - <meta http-equiv="Content-Type" content="text/html; charset=gb2312">
 - </head>
 - <body >
 - <table $htmlformat[0] height="100%" border="0" width="100%" cellpadding="10" cellspacing="0" bgcolor="#eeeeee" style="border:1px solid #000000">
 - <tr>
 - <td width="100%" valign="middle" align="left">
 - <span style="color: $htmlformat[1];font-size: $htmlformat[2]">$htmlformat[3]</span>
 - </td>
 - </tr>
 - </table>
 - </body>
 - </html>
 
2.asp.net代碼:
- //---------------------讀html模板頁面到stringbuilder對象里----
 - string[] format=new string[4];//定義和htmlyem標(biāo)記數(shù)目一致的數(shù)組
 - StringBuilder htmltext=new StringBuilder();
 - try
 - {
 - using (StreamReader sr = new StreamReader("存放模板頁面的路徑和頁面名"))
 - {
 - String line;
 - while ((line = sr.ReadLine()) != null)
 - {
 - htmltext.Append(line);
 - }
 - sr.Close();
 - }
 - }
 - catch
 - {
 - Response.Write("<Script>alert('讀取文件錯誤')</Script>");
 - }
 - //---------------------給標(biāo)記數(shù)組賦值------------
 - format[0]="background="bg.jpg"";//背景圖片
 - format[1]= "#990099";//字體顏色
 - format[2]="150px";//字體大小
 - format[3]= "<marquee>生成的模板html頁面</marquee>";//文字說明
 - //----------替換htm里的標(biāo)記為你想加的內(nèi)容
 - for(int i=0;i<4;i++)
 - {
 - htmltext.Replace("$htmlformat["+i+"]",format[i]);
 - }
 - //----------生成htm文件------------------――
 - try
 - {
 - using(StreamWriter sw=new StreamWriter("存放路徑和頁面名",false,System.Text.Encoding.GetEncoding("GB2312")))
 - {
 - sw.WriteLine(htmltext);
 - sw.Flush();
 - sw.Close();
 - }
 - }
 - catch
 - {
 - Response.Write ("The file could not be wirte:");
 - }
 
小結(jié)
用此方法可以方便的生成html文件。程序使用了是循環(huán)替換,因此對需替換大量元素的模板速度非??臁?/P>
【編輯推薦】















 
 
 
 
 
 
 