网创优客建站品牌官网
为成都网站建设公司企业提供高品质网站建设
热线:028-86922220
成都专业网站建设公司

定制建站费用3500元

符合中小企业对网站设计、功能常规化式的企业展示型网站建设

成都品牌网站建设

品牌网站建设费用6000元

本套餐主要针对企业品牌型网站、中高端设计、前端互动体验...

成都商城网站建设

商城网站建设费用8000元

商城网站建设因基本功能的需求不同费用上面也有很大的差别...

成都微信网站建设

手机微信网站建站3000元

手机微信网站开发、微信官网、微信商城网站...

建站知识

当前位置:首页 > 建站知识

asp.net基于替换模版页的形式如何生成静态页-创新互联

小编给大家分享一下asp.net基于替换模版页的形式如何生成静态页,相信大部分人都还不怎么了解,因此分享这篇文章给大家参考一下,希望大家阅读完这篇文章后大有收获,下面让我们一起去了解一下吧!

创新互联是一家集网站建设,怀远企业网站建设,怀远品牌网站建设,网站定制,怀远网站建设报价,网络营销,网络优化,怀远网站推广为一体的创新建站企业,帮助传统企业提升企业形象加强企业竞争力。可充分满足这一群体相比中小企业更为丰富、高端、多元的互联网需求。同时我们时刻保持专业、时尚、前沿,时刻以成就客户成长自我,坚持不断学习、思考、沉淀、净化自己,让我们为更多的企业打造出实用型网站。

具体如下:

第一步:新建项目,创建一个简单模版页:TemplatePage.htm




  Porschev 生成静态页简单示例


$Porschev[0]$

  • 页标题:$Porschev[0]$
  • 名称:$Porschev[1]$
  • 网址:$Porschev[2]$
  • 时间:$Porschev[3]$
  • 详述:$Porschev[4]$

第二步:创建一个config文件:CreateHtml.config



 
 
 
 
 

第三步:编写生成静态页代码:(添加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##读取配置文件某节点的个数
    ///
    /// 读取配置文件某节点的个数
    ///
    ///配置文件的路径
    ///要获取的节点
    ///返回节点个数
    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##创建文件夹
    ///
    /// 创建文件夹
    ///
    ///要创建的路径
    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页
    ///
    /// 生成HTML页
    ///
    ///配置文件的路径
    ///配置文件节点名
    ///模版页路径
    ///替换数组
    ///生成HTML路径
    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##生成静态页
///
/// 生成静态页
///
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基于替换模版页的形式如何生成静态页”这篇文章的所有内容,感谢各位的阅读!相信大家都有了一定的了解,希望分享的内容对大家有所帮助,如果还想学习更多知识,欢迎关注创新互联行业资讯频道!


标题名称:asp.net基于替换模版页的形式如何生成静态页-创新互联
文章源于:http://bjjierui.cn/article/dcjhdg.html

其他资讯