經(jīng)過驗(yàn)證 下面大致5個(gè)方法均可以成功轉(zhuǎn)化,但是轉(zhuǎn)化過程中一定要注意原ASPX文件的編碼類型,如果是GB2312的 那么不管是在streamReader的時(shí)候,還是先將原數(shù)據(jù)轉(zhuǎn)化成byte流,再轉(zhuǎn)化出HTML字符串的時(shí)候一定要注意同原ASPX頁面編碼類型相同,否則將出現(xiàn)亂碼。 using System.Text; using System.IO; public partial class _Default : System.Web.UI.Page { public StreamWriter sw; protected void Page_Load(object sender, EventArgs e) { WebClient myWebClient = new WebClient(); myWebClient.Credentials = CredentialCache.DefaultCredentials; byte[] pagedata = myWebClient.DownloadData("http://home./default.aspx"); string myDataBuffer = Encoding.Default.GetString(pagedata); string path = HttpContext.Current.Server.MapPath("."); Encoding code = Encoding.GetEncoding("gb2312"); string htmlfilename = "test.html"; try { FileStream fs = new FileStream(path+"/"+htmlfilename, FileMode.Create, FileAccess.Write); StreamWriter sw = new StreamWriter(fs, Encoding.Default); sw.WriteLine(myDataBuffer); sw.Close(); fs.Close(); Response.Write("生成成功了!ok"); } catch (Exception ex) { File.Delete(path + htmlfilename); HttpContext.Current.Response.Write(ex.Message); HttpContext.Current.Response.End(); Response.Write("生成失敗了!no"); } finally { if (sw != null) sw.Close(); } } }
我們開發(fā)的asp.net系統(tǒng)中,有些動(dòng)態(tài)的頁面常被頻繁,如我們的首頁index.aspx它涉及到大量的數(shù)據(jù)庫查詢工作,當(dāng)不斷有用戶它時(shí),服務(wù)器便不斷向數(shù)據(jù)庫的查詢,實(shí)際上做了許多重復(fù)的工作 服務(wù)器端的myPage.aspx 客戶端顯示myPage.htm 客戶端 針對(duì)這種資源的浪費(fèi)情況,我們現(xiàn)在來設(shè)計(jì)一個(gè)解決方案。我們先將那些一段時(shí)間內(nèi)內(nèi)容不會(huì)有什么改變,但又遭大量的動(dòng)態(tài)頁面生成靜態(tài)的頁面存放在服務(wù)器上,當(dāng)客戶端發(fā)出請(qǐng)求時(shí),就讓他們直接我們生成的靜態(tài)頁面,過程如下圖。 客戶端顯示myPage.htm 客戶端 Execute 服務(wù)器端的myPage.aspx 服務(wù)器端的myPage.htm 現(xiàn)在我們需要一個(gè)后臺(tái)程序來完成這些事情。 我們可將此做成一個(gè)類classAspxToHtml ,其代碼 using System; using System.IO; using System.Web.UI; namespace LeoLu { /// summary /// AspxToHtml 的摘要說明。 /// /summary public class AspxToHtml { /// summary /// Aspx文件url /// /summary public string strUrl; /// summary /// 生成html文件的保存路徑 /// /summary public string strSavePath; /// summary /// 生成html文件的文件名 /// /summary public string strSaveFile; public AspxToHtml() { // // TOD 在此處添加構(gòu)造函數(shù)邏輯 // } /// summary /// 將strUrl放到strSavePath目錄下,保存為strSaveFile /// /summary /// returns是否成功/returns public bool ExecAspxToHtml() { try { StringWriter strHTML = new StringWriter(); System.Web.UI.Page myPage = new Page(); //System.Web.UI.Page中有個(gè)Server對(duì)象,我們要利用一下它 myPage.Server.Execute(strUrl,strHTML);//將asp_net.aspx將在客戶段顯示的html內(nèi)容讀到了strHTML中 StreamWriter sw = new StreamWriter(strSavePath+strSaveFile,true,System.Text.Encoding.GetEncoding("GB2312")); //新建一個(gè)文件Test.htm,文件格式為GB2312 sw.Write(strHTML.ToString()); //將strHTML中的字符寫到Test.htm中 strHTML.Close();//關(guān)閉StringWriter sw.Close();//關(guān)閉StreamWriter return true; } catch { return false; } } /// summary /// 將Url放到Path目錄下,保存為FileName /// /summary /// param name="Url"aspx頁面url/param /// param name="Path"生成html文件的保存路徑/param /// param name="FileName"生成html文件的文件名/param /// returns/returns public bool ExecAspxToHtml(string Url,string Path,string FileName) { try { StringWriter strHTML = new StringWriter(); System.Web.UI.Page myPage = new Page(); //System.Web.UI.Page中有個(gè)Server對(duì)象,我們要利用一下它 myPage.Server.Execute(Url,strHTML); //將asp_net.aspx將在客戶段顯示的html內(nèi)容讀到了strHTML中 StreamWriter sw = new StreamWriter(Path+FileName,true,System.Text.Encoding.GetEncoding("GB2312")); //新建一個(gè)文件Test.htm,文件格式為GB2312 sw.Write(strHTML.ToString()); //將strHTML中的字符寫到Test.htm中 strHTML.Close();//關(guān)閉StringWriter sw.Close();//關(guān)閉StreamWriter return true; } catch { return false; } } } }
轉(zhuǎn)自http://www.cnblogs.com/chengyan/archive/2010/12/21/1912335.html 其他 方法: ------------------------------
using system.net; First:在服務(wù)器上指定aspx網(wǎng)頁,生成html靜態(tài)頁 public partial class Default2 : System.Web.UI.Page Second:在服務(wù)器上執(zhí)行aspx網(wǎng)頁時(shí)在page_render事件里將本頁面生成html靜態(tài)頁 public partial class Default3 : System.Web.UI.Page Third:從指定連接獲取源代碼生成html靜態(tài)頁 public partial class Default4 : System.Web.UI.Page |
|