現(xiàn)有動(dòng)態(tài)頁(yè)面的格式都是類(lèi)似pageName.aspx?ID=1的格式,后面由于發(fā)布服務(wù)器的原因,要求將動(dòng)態(tài)頁(yè)面轉(zhuǎn)為靜態(tài)html后上傳。
首先根據(jù)頁(yè)面生成的格式,枚舉獲取頁(yè)面html:
1 foreach (var page in pageList)2 {3 string html = ReaDHTML(string.Format("pageName.aspx?ID={0}", page.ID));4 html = ReplaceListAndSingle(html);5 WriteHtml(string.Format("pageName_{0}.html", page.ID), html);6 }
讀取asp.net頁(yè)面:
public static string ReadHtml(string sourceurl) { try { System.Net.WebRequest myRequest = System.Net.WebRequest.Create(sourceurl); System.Net.WebResponse myResponse = myRequest.GetResponse(); using (Stream stream = myResponse.GetResponseStream()) { using (StreamReader sr = new StreamReader(stream, Encoding.GetEncoding("utf-8"))) { return sr.ReadToEnd(); } } } catch (Exception ex) { throw new Exception(string.Format("ReadHtml出錯(cuò):{0}", ex.Message)); } }
使用正則替換頁(yè)面內(nèi)的動(dòng)態(tài)鏈接:
public static string ReplaceListAndSingle(string html) { html = Regex.Replace(html, @"_list/.aspx/?ID=(/d*)", "_list_$1.html", RegexOptions.IgnoreCase); html = Regex.Replace(html, @"_single/.aspx/?ID=(/d*)", "_single_$1.html", RegexOptions.IgnoreCase); return html.Replace(".aspx", ".html"); }
動(dòng)態(tài)頁(yè)面格式為 pageName_list.aspx?ID=1 與 pageName_single.aspx?ID=1,正則替換后變?yōu)殪o態(tài)格式:pageName_list_1.html 與pageName_single_1.html。
將替換后的頁(yè)面html寫(xiě)入文件:
public static bool WriteHtml(string url, string html) { try { using (StreamWriter sw = new StreamWriter(HttpContext.Current.Server.MapPath(url), false, Encoding.GetEncoding("utf-8"))) { sw.WriteLine(html); sw.Close(); } return true; } catch (Exception ex) { throw new Exception(string.Format("WriteHtml出錯(cuò):{0}", ex.Message)); } }
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注