国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 學(xué)院 > 開(kāi)發(fā)設(shè)計(jì) > 正文

ASP.NET 根據(jù)現(xiàn)有動(dòng)態(tài)頁(yè)面生成靜態(tài)Html

2019-11-17 02:04:02
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

asp.net 根據(jù)現(xiàn)有動(dòng)態(tài)頁(yè)面生成靜態(tài)Html

現(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)); }        }


發(fā)表評(píng)論 共有條評(píng)論
用戶(hù)名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 德惠市| 石阡县| 杭锦旗| 民丰县| 临猗县| 漠河县| 惠东县| 洛阳市| 卢龙县| 桂阳县| 大关县| 建湖县| 乃东县| 敦煌市| 张北县| 蚌埠市| 汝州市| 台东市| 宁南县| 赤水市| 右玉县| 汉源县| 黄冈市| 吴江市| 惠水县| SHOW| 凌云县| 永吉县| 丰镇市| 香河县| 石林| 平泉县| 辛集市| 东丽区| 高台县| 勐海县| 绥德县| 获嘉县| 蒲江县| 贺兰县| 五台县|