ASP.net(c#) 生成html的幾種解決方案[思路]第1/2頁
2024-07-10 12:38:56
供稿:網友
方案1:
代碼如下:
/// <summary >
/// 傳入URL返回網頁的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 "";
}
你可以用這個函數獲取網頁的客戶端的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
以下是程序代碼 注:此代碼非原創!參考別人代碼
代碼如下:
//生成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";
// 替換內容
// 這時,模板文件已經讀入到名稱為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();