使用ASP.NET模板生成HTML靜態(tài)頁(yè)面的五種方案
2024-07-10 12:41:42
供稿:網(wǎng)友
ASP.NET模版生成HTML靜態(tài)頁(yè)面方案1:
代碼如下:
/// < summary>
/// 傳入U(xiǎn)RL返回網(wǎng)頁(yè)的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 "";
}
你可以用這個(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中涉及此類(lèi)操作的是System.IO
以下是程序代碼 注:此代碼非原創(chuàng)!參考別人代碼
代碼如下:
//生成HTML頁(yè)
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)容
// 這時(shí),模板文件已經(jīng)讀入到名稱為str的變量中了
str =str.Replace("ShowArticle",strText); //模板頁(yè)中的ShowArticle
str = str.Replace("biaoti",strText);
str = str.Replace("content",strContent);
str = str.Replace("author",strAuthor);
// 寫(xiě)文件
try
{
sw = new StreamWriter(path + htmlfilename , false, code);
sw.Write(str);
sw.Flush();
}
catch(Exception ex)