asp.net基于替換模版頁(yè)的形式生成靜態(tài)頁(yè)的方法
本文實(shí)例講述了asp.net基于替換模版頁(yè)的形式生成靜態(tài)頁(yè)的方法。分享給大家供大家參考,具體如下:
第一步:新建項(xiàng)目,創(chuàng)建一個(gè)簡(jiǎn)單模版頁(yè):TemplatePage.htm
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>Porschev 生成靜態(tài)頁(yè)簡(jiǎn)單示例</title> </head> <body> <h1>$Porschev[0]$</h1> <ul> <li>頁(yè)標(biāo)題:$Porschev[0]$</li> <li>名稱(chēng):$Porschev[1]$</li> <li>網(wǎng)址:<a href="$Porschev[2]$" target="_blank">$Porschev[2]$</a></li> <li>時(shí)間:$Porschev[3]$</li> <li>詳述:$Porschev[4]$</li> </ul> </body> </html>
第二步:創(chuàng)建一個(gè)config文件:CreateHtml.config
<?xml version="1.0" encoding="utf-8" ?> <web> <website key="0" value="title"/> <website key="1" value="name"/> <website key="2" value="url"/> <website key="3" value="createDate"/> <website key="4" value="desc"/> </web>
第三步:編寫(xiě)生成靜態(tài)頁(yè)代碼:(添加System.Web引用)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; using System.Xml; namespace CreateHtmlBLL { public class CreateHtmlBLL { #region##讀取配置文件某節(jié)點(diǎn)的個(gè)數(shù) ///<summary> /// 讀取配置文件某節(jié)點(diǎn)的個(gè)數(shù) ///</summary> ///<param name="path">配置文件的路徑</param> ///<param name="nodeName">要獲取的節(jié)點(diǎn)</param> ///<returns>返回節(jié)點(diǎn)個(gè)數(shù)</returns> private int ReadConfig(string path,string nodeName) { string absoPath = string.Empty; //絕對(duì)路徑 try { absoPath = System.Web.HttpContext.Current.Server.MapPath(path); XmlDocument xd = new XmlDocument(); xd.Load(absoPath); XmlNodeList nodeList = xd.SelectNodes(nodeName); //得到相應(yīng)節(jié)點(diǎn)的集合 return nodeList.Count; } catch (Exception) { throw; } } #endregion #region##創(chuàng)建文件夾 ///<summary> /// 創(chuàng)建文件夾 ///</summary> ///<param name="path">要?jiǎng)?chuàng)建的路徑</param> public void CreatFolder(string path) { string absoPath = string.Empty; //絕對(duì)路徑 try { absoPath = System.Web.HttpContext.Current.Server.MapPath(path); if (!Directory.Exists(absoPath)) { Directory.CreateDirectory(absoPath); } } catch (Exception) { throw; } } #endregion #region##生成HTML頁(yè) ///<summary> /// 生成HTML頁(yè) ///</summary> ///<param name="configPath">配置文件的路徑</param> ///<param name="configNodeName">配置文件節(jié)點(diǎn)名</param> ///<param name="temPath">模版頁(yè)路徑</param> ///<param name="arr">替換數(shù)組</param> ///<param name="createPath">生成HTML路徑</param> public void CreateHtml(string configPath, String configNodeName, string temPath, string[] arr,string createPath) { string fileName = string.Empty; //生成文件名 string absoCrePath = string.Empty; //生成頁(yè)絕對(duì)路徑 string absoTemPath = string.Empty; //模版頁(yè)的絕對(duì)中徑 int nodeCount = 0; //節(jié)點(diǎn)數(shù) try { absoCrePath = System.Web.HttpContext.Current.Server.MapPath(createPath); absoTemPath = System.Web.HttpContext.Current.Server.MapPath(temPath); nodeCount = ReadConfig(configPath, configNodeName); FileStream fs = File.Open(absoTemPath, FileMode.Open, FileAccess.Read); //讀取模版頁(yè) StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("utf-8")); StringBuilder sb = new StringBuilder(sr.ReadToEnd()); sr.Close(); sr.Dispose(); for (int i = 0; i < nodeCount; i++) { sb.Replace("$Porschev[" + i + "]$", arr[i]); } CreatFolder(createPath); fileName = DateTime.Now.ToFileTime().ToString() + ".html"; //設(shè)置文件名(這里可以根據(jù)需要變化命名) FileStream cfs = File.Create(absoCrePath + "/" + fileName); StreamWriter sw = new StreamWriter(cfs, Encoding.GetEncoding("utf-8")); sw.Write(sb.ToString()); sw.Flush(); sw.Close(); sw.Dispose(); } catch (Exception) { throw; } } #endregion } }
第四步:測(cè)式生成
protected void Page_Load(object sender, EventArgs e) { CreateHtml(); } #region##生成靜態(tài)頁(yè) ///<summary> /// 生成靜態(tài)頁(yè) ///</summary> public void CreateHtml() { try { string[] arr = new string[5]; arr[0] = "Porschev 靜態(tài)頁(yè)測(cè)式"; arr[1] = "dtan"; arr[2] = "www.dbjr.com.cn"; arr[3] = DateTime.Today.ToString(); arr[4] = "歡迎來(lái)到腳本之家"; CreateHtmlBLL.CreateHtmlBLL chb = new CreateHtmlBLL.CreateHtmlBLL(); chb.CreateHtml("CreateHtml.config", "web/website","Template/TemplatePage.htm", arr,"Porschev"); } catch (Exception ex) { throw ex; } } #endregion
更多關(guān)于asp.net相關(guān)內(nèi)容感興趣的讀者可查看本站專(zhuān)題:《asp.net操作json技巧總結(jié)》、《asp.net字符串操作技巧匯總》、《asp.net操作XML技巧總結(jié)》、《asp.net文件操作技巧匯總》、《asp.net ajax技巧總結(jié)專(zhuān)題》及《asp.net緩存操作技巧總結(jié)》。
希望本文所述對(duì)大家asp.net程序設(shè)計(jì)有所幫助。
- ASP.NET AJAX 4.0的模版編程(Template Programming)介紹
- asp.net實(shí)現(xiàn)生成靜態(tài)頁(yè)并添加鏈接的方法
- Asp.Net生成靜態(tài)頁(yè)面的實(shí)現(xiàn)方法
- ASP.NET動(dòng)態(tài)生成靜態(tài)頁(yè)面的實(shí)例代碼
- ASP.NET MVC3關(guān)于生成純靜態(tài)后如何不再走路由直接訪問(wèn)靜態(tài)頁(yè)面
- 使用ASP.NET模板生成HTML靜態(tài)頁(yè)面的五種方案
- asp.net 生成靜態(tài)頁(yè)筆記
- Asp.Net 生成靜態(tài)頁(yè)并實(shí)現(xiàn)分頁(yè)效果
- Asp.NET 生成靜態(tài)頁(yè)面并分頁(yè)的代碼
- ASP.NET 生成靜態(tài)頁(yè)面 實(shí)現(xiàn)思路
- asp.net 生成靜態(tài)頁(yè)時(shí)的進(jìn)度條顯示
- asp.net生成靜態(tài)頁(yè)并分頁(yè)+ubb
相關(guān)文章
WPF在自定義文本框中實(shí)現(xiàn)輸入法跟隨光標(biāo)
本文主要為大家介紹了如何在WPF寫(xiě)一個(gè)自定義的文本框,并且能實(shí)現(xiàn)讓輸入法跟隨光標(biāo)。文中的示例代碼講解詳細(xì),需要的可以參考一下2022-02-02IIS處理Asp.net請(qǐng)求和Asp.net頁(yè)面生命周期說(shuō)明
當(dāng)一個(gè)客戶端頁(yè)面訪問(wèn)IIS試圖獲取一些信息的時(shí)候,發(fā)生了什么事情?一個(gè)請(qǐng)求在通過(guò)了HTTP管道后又發(fā)生了什么?本文主要是描述這兩個(gè)過(guò)程,即IIS處理asp.net請(qǐng)求和asp.net的頁(yè)面生命周期。歡迎大家積極拍磚,共同學(xué)習(xí),共同進(jìn)步。2011-05-05Asp.Net Core 通過(guò)中間件防止圖片盜鏈的實(shí)例
本篇文章主要介紹了Asp.Net Core 通過(guò)中間件防止圖片盜鏈的實(shí)例,具有一定的參考價(jià)值,有興趣的可以了解一下。2016-12-12asp.net開(kāi)發(fā)與web標(biāo)準(zhǔn)的沖突問(wèn)題的一些常見(jiàn)解決方法
Visual Studio .net從2003到現(xiàn)在的2008,一路走來(lái)慢慢強(qiáng)大……從以前的vs2003能自動(dòng)改亂你的html代碼到現(xiàn)在在vs2008中都能直接對(duì)html代碼進(jìn)行w3c標(biāo)準(zhǔn)驗(yàn)證并提示了,非常不易。2009-02-02基于ASP.NET Core數(shù)據(jù)保護(hù)生成驗(yàn)證token示例
本篇文章主要介紹了基于ASP.NET Core數(shù)據(jù)保護(hù)生成驗(yàn)證token,小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2017-02-02使用HttpWebRequest向網(wǎng)站模擬上傳數(shù)據(jù)
使用HttpWebRequest向網(wǎng)站模擬上傳數(shù)據(jù)...2006-09-09完美解決Could not load file or assembly AjaxPro.2 or one of its
Could not load file or assembly AjaxPro.2,經(jīng)排查原來(lái)是mcafee限制了2007-08-08asp.net 數(shù)據(jù)庫(kù)備份還原(sqlserver+access)
Asp.net 備份、還原Ms SQLServer及壓縮Access數(shù)據(jù)庫(kù)2008-11-11SqlCommandBuilder類(lèi)批量更新excel或者CSV數(shù)據(jù)的方法
這篇文章主要介紹了SqlCommandBuilder類(lèi)批量更新excel或者CSV數(shù)據(jù)的方法,需要的朋友可以參考下2015-10-10