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

首頁 > 編程 > .NET > 正文

asp.net基于替換模版頁的形式生成靜態頁的方法

2024-07-10 12:53:59
字體:
來源:轉載
供稿:網友

asp.net是微軟推出的新一代腳本語言。asp.net是在web開發平臺進行開發,所以.NETFramework框架吸收了以前版本ASP的最大優點,同時也糾正了錯誤,那么asp.net基于替換模版頁的形式生成靜態頁的方法有哪些呢?錯新技術頻道帶大家來看看吧!

第一步:新建項目,創建一個簡單模版頁: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 生成靜態頁簡單示例</title></head><body><h1>$Porschev[0]$</h1><ul><li>頁標題:$Porschev[0]$</li><li>名稱:$Porschev[1]$</li><li>網址:<a href="$Porschev[2]$" target="_blank">$Porschev[2]$</a></li><li>時間:$Porschev[3]$</li><li>詳述:$Porschev[4]$</li></ul></body></html>

第二步:創建一個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>

第三步:編寫生成靜態頁代碼:(添加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##讀取配置文件某節點的個數    ///<summary>    /// 讀取配置文件某節點的個數    ///</summary>    ///<param name="path">配置文件的路徑</param>    ///<param name="nodeName">要獲取的節點</param>    ///<returns>返回節點個數</returns>    private int ReadConfig(string path,string nodeName)    {      string absoPath = string.Empty; //絕對路徑      try      {        absoPath = System.Web.HttpContext.Current.Server.MapPath(path);        XmlDocument xd = new XmlDocument();        xd.Load(absoPath);        XmlNodeList nodeList = xd.SelectNodes(nodeName); //得到相應節點的集合        return nodeList.Count;      }      catch (Exception)      {        throw;      }    }    #endregion    #region##創建文件夾    ///<summary>    /// 創建文件夾    ///</summary>    ///<param name="path">要創建的路徑</param>    public void CreatFolder(string path)    {      string absoPath = string.Empty; //絕對路徑      try      {        absoPath = System.Web.HttpContext.Current.Server.MapPath(path);        if (!Directory.Exists(absoPath))        {          Directory.CreateDirectory(absoPath);        }      }      catch (Exception)      {        throw;      }    }    #endregion    #region##生成HTML頁    ///<summary>    /// 生成HTML頁    ///</summary>    ///<param name="configPath">配置文件的路徑</param>    ///<param name="configNodeName">配置文件節點名</param>    ///<param name="temPath">模版頁路徑</param>    ///<param name="arr">替換數組</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;   //生成頁絕對路徑      string absoTemPath = string.Empty;   //模版頁的絕對中徑      int nodeCount = 0;           //節點數      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); //讀取模版頁        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";     //設置文件名(這里可以根據需要變化命名)        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  }}

第四步:測式生成

protected void Page_Load(object sender, EventArgs e){    CreateHtml();}#region##生成靜態頁///<summary>/// 生成靜態頁///</summary>public void CreateHtml(){    try    {      string[] arr = new string[5];      arr[0] = "Porschev 靜態頁測式";      arr[1] = "dtan";      arr[2] = "www.jb51.net";      arr[3] = DateTime.Today.ToString();      arr[4] = "歡迎來到錯新技術頻道";      CreateHtmlBLL.CreateHtmlBLL chb = new CreateHtmlBLL.CreateHtmlBLL();      chb.CreateHtml("CreateHtml.config", "web/website","Template/TemplatePage.htm", arr,"Porschev");    }    catch (Exception ex)    {      throw ex;    }}#endregion以上就是asp.net基于替換模版頁的形式生成靜態頁的方法,你學會了嗎?如果你覺得錯新技術頻道可以幫助到你,建議你好好收藏哦。
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
主站蜘蛛池模板: 铜鼓县| 永嘉县| 乐业县| 田东县| 凤冈县| 渝北区| 金门县| 苗栗市| 彭州市| 玛曲县| 山西省| 泗水县| 晋中市| 探索| 澎湖县| 丰县| 阳曲县| 习水县| 黎平县| 新和县| 临清市| 正蓝旗| 东乡族自治县| 冀州市| 敦煌市| 蒙山县| 衢州市| 惠东县| 黄龙县| 阿荣旗| 海南省| 陆河县| 延边| 开平市| 田阳县| 宁安市| 扶风县| 阳信县| 贵阳市| 玛曲县| 潮州市|