asp.net基于替换模版页的形式生成静态页的方法

所属分类: 网络编程 / ASP.NET 阅读数: 1819
收藏 0 赞 0 分享

本文实例讲述了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相关内容感兴趣的读者可查看本站专题:《asp.net操作json技巧总结》、《asp.net字符串操作技巧汇总》、《asp.net操作XML技巧总结》、《asp.net文件操作技巧汇总》、《asp.net ajax技巧总结专题》及《asp.net缓存操作技巧总结》。

希望本文所述对大家asp.net程序设计有所帮助。

更多精彩内容其他人还在看

asp.net 页面间传值与跳转的区别

通过Server.Transfer("b.aspx") 与Response.Redirect("b.aspx")的区别
收藏 0 赞 0 分享

ASP.NET Gridview与checkbox全选、全不选实现代码

ASP.NET Gridview checkbox全选与全不选实现代码,其实原理就是利用js来实现的,但需要简单的设置下回传。
收藏 0 赞 0 分享

ASP.NET DropDownList控件的使用方法

ASP.NET DropDownList控件的使用方法,学习asp.net的朋友没用过这个控件的朋友可以参考下。
收藏 0 赞 0 分享

一些.NET对多线程异常处理技巧分享

多线程应用,在实际的项目或产品开发中,原则上来说,应该尽量避免,但是在强调用户体验的要求下或开发平台的限制下(如 Silverlight Socket 通讯),我们不得不用多线程。
收藏 0 赞 0 分享

ASP.NET MVC运行出现Uncaught TypeError: Cannot set property __MVC_FormValidation of null的解决方法

同一相站点,有些页面的客户端验证能工作,而有些死活不行。打开页面就出现Uncaught TypeError: Cannot set property __MVC_FormValidation of null错误
收藏 0 赞 0 分享

asp.net 通用分页显示辅助类(改进版)

在使用ASP.NET编程时,如果不用控件的默认分页功能,想自己搞一个,可以看看本文的asp.net通用分页显示辅助类哦。
收藏 0 赞 0 分享

微软 Visual Studio 2010官方下载地址给大家

昨天VS2010在网上报道都已经发布了,现在今天在网上找到Visual Studio 2010官方下载地址,提供给大家下载。
收藏 0 赞 0 分享

Javascript 直接调用服务器C#代码 ASP.NET Ajax实例

近来总有一些朋友会问到一些入门的问题,把这些问题整理一下,写出来。在以前的文章里,曾经利用纯JS编写过Ajax引擎,在真正开发的时候,大家都不喜欢以这种低效率的方式开发,利用MS Ajax的集成的引擎,可以简单不少工作。
收藏 0 赞 0 分享

ASP.NET 页面刷新的实现方法(包括html,js)

ASP.NET 页面刷新的实现方法,比较全了, 包括html与js下的实现方法。
收藏 0 赞 0 分享

asp.net 无刷新翻页就是这么简单

前两天看了一个自定义分页控件,和AspNetPager一样是实现IPostBackEventHandler接口,不过简洁许多,就想能不能实现ICallbackEventHandler接口做到无刷新分页呢?想到了就马上去做,终于,设想变成了现实!!
收藏 0 赞 0 分享
查看更多