国产探花免费观看_亚洲丰满少妇自慰呻吟_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ā)表
主站蜘蛛池模板: 宁强县| 聊城市| 日喀则市| 大化| 扬州市| 乃东县| 龙陵县| 凌云县| 清丰县| 天台县| 融水| 山阴县| 铁岭市| 长白| 政和县| 池州市| 锡林浩特市| 汪清县| 平顺县| 长泰县| 博乐市| 仪征市| 太谷县| 平谷区| 龙泉市| 东阳市| 锦州市| 洪湖市| 太仓市| 磴口县| 土默特左旗| 修武县| 合作市| 嘉峪关市| 吕梁市| 治多县| 连云港市| 丹东市| 阿巴嘎旗| 长宁县| 亳州市|