asp.net基于替換模版頁(yè)的形式如何生成靜態(tài)頁(yè)-創(chuàng)新互聯(lián)

小編給大家分享一下asp.net基于替換模版頁(yè)的形式如何生成靜態(tài)頁(yè),相信大部分人都還不怎么了解,因此分享這篇文章給大家參考一下,希望大家閱讀完這篇文章后大有收獲,下面讓我們一起去了解一下吧!

創(chuàng)新互聯(lián)是一家集網(wǎng)站建設(shè),懷遠(yuǎn)企業(yè)網(wǎng)站建設(shè),懷遠(yuǎn)品牌網(wǎng)站建設(shè),網(wǎng)站定制,懷遠(yuǎn)網(wǎng)站建設(shè)報(bào)價(jià),網(wǎng)絡(luò)營(yíng)銷,網(wǎng)絡(luò)優(yōu)化,懷遠(yuǎn)網(wǎng)站推廣為一體的創(chuàng)新建站企業(yè),幫助傳統(tǒng)企業(yè)提升企業(yè)形象加強(qiáng)企業(yè)競(jìng)爭(zhēng)力??沙浞譂M足這一群體相比中小企業(yè)更為豐富、高端、多元的互聯(lián)網(wǎng)需求。同時(shí)我們時(shí)刻保持專業(yè)、時(shí)尚、前沿,時(shí)刻以成就客戶成長(zhǎng)自我,堅(jiān)持不斷學(xué)習(xí)、思考、沉淀、凈化自己,讓我們?yōu)楦嗟钠髽I(yè)打造出實(shí)用型網(wǎng)站。

具體如下:

第一步:新建項(xiàng)目,創(chuàng)建一個(gè)簡(jiǎn)單模版頁(yè):TemplatePage.htm

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"/tupian/20230522/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
  <title>Porschev 生成靜態(tài)頁(yè)簡(jiǎn)單示例</title>
</head>
<body>
<h2>$Porschev[0]$</h2>
<ul>
<li>頁(yè)標(biāo)題:$Porschev[0]$</li>
<li>名稱:$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.jb51.net";
      arr[3] = DateTime.Today.ToString();
      arr[4] = "歡迎來(lái)到創(chuàng)新互聯(lián)網(wǎng)站建設(shè)公司,";
      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基于替換模版頁(yè)的形式如何生成靜態(tài)頁(yè)”這篇文章的所有內(nèi)容,感謝各位的閱讀!相信大家都有了一定的了解,希望分享的內(nèi)容對(duì)大家有所幫助,如果還想學(xué)習(xí)更多知識(shí),歡迎關(guān)注創(chuàng)新互聯(lián)行業(yè)資訊頻道!

當(dāng)前題目:asp.net基于替換模版頁(yè)的形式如何生成靜態(tài)頁(yè)-創(chuàng)新互聯(lián)
網(wǎng)頁(yè)鏈接:http://muchs.cn/article16/dcjhdg.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供靜態(tài)網(wǎng)站、網(wǎng)站收錄、商城網(wǎng)站、響應(yīng)式網(wǎng)站微信公眾號(hào)、網(wǎng)站導(dǎo)航

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)

搜索引擎優(yōu)化