替换式的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.111cn.net";
            arr[3] = DateTime.Today.ToString();
            arr[4] = "上班时间上CSDN,都是不好好工作的闲人。。。";
            CreateHtmlBLL.CreateHtmlBLL chb = new CreateHtmlBLL.CreateHtmlBLL();
            chb.CreateHtml("CreateHtml.config", "web/website","Template/TemplatePage.htm", arr,"Porschev");
        }
        catch (Exception ex)
        {           
            throw ex;
        }
    }
    #endregion
时间: 2024-08-01 10:28:41

替换式的asp.net生成静态页方法的相关文章

asp.net 生成静态页笔记

1.使用serever.Excute 复制代码 代码如下: StreamWriter sw = new StreamWriter(Server.MapPath("html/Login.html"), false); Server.Execute("ShowColumn.aspx?id=1&page=2", sw); sw.Close(); 2.替换字符 url重写 1.定义重写规则 urls.xml 变成urls.config 复制代码 代码如下: <

asp.net 生成静态页笔记_实用技巧

1.使用serever.Excute 复制代码 代码如下: StreamWriter sw = new StreamWriter(Server.MapPath("html/Login.html"), false); Server.Execute("ShowColumn.aspx?id=1&page=2", sw); sw.Close(); 2.替换字符 url重写 1.定义重写规则 urls.xml 变成urls.config 复制代码 代码如下: <

ASP.NET生成静态页技术

模板页面 <!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>企业堂 - ASP.N

教你用Asp.NET 生成静态页

asp.net|静态     环境:Microsoft .NET Framework SDK v1.1 OS:Windows Server 2003 中文版ASP.Net生成静态HTML页在Asp中实现的生成静态页用到的FileSystemObject对象!     在.Net中涉及此类操作的是System.IO以下是程序代码 注:此代码非原创!参考别人代码     CODE://生成HTML页public static bool WriteFile(string strText,string

教你用Asp.NET生成静态页

asp.net|静态     环境:Microsoft .NET Framework SDK v1.1 OS:Windows Server 2003 中文版ASP.Net生成静态HTML页在Asp中实现的生成静态页用到的FileSystemObject对象!    在.Net中涉及此类操作的是System.IO以下是程序代码 注:此代码非原创!参考别人代码     CODE://生成HTML页public static bool WriteFile(string strText,string s

asp.net 生成静态页时的进度条显示_实用技巧

asp.net如何生成静态页,请参考下面的文章:http://www.jb51.net/article/18175.htm而我们用模拟的话,只需要让线程延迟执行就可以了.比如下面的代码: 复制代码 代码如下: for (int i = 0; i < 10; i++) { DateTime startTime = DateTime.Now; Response.Write(i + "-------------执行时间:" + startTime.ToString()+"&l

asp.net生成静态页

asp.net|静态 //生成HTML页  public static bool WriteFile(string strText,string strContent,string strAuthor)   {   string path = HttpContext.Current.Server.MapPath("/news/");   Encoding code = Encoding.GetEncoding("gb2312");   // 读取模板文件   str

php生成静态页方法

  一,PHP脚本与动态页面. PHP脚本是一种服务器端脚本程序,可通过嵌入等方法与html文件混合,也可以类,函数封装等形式,以模板的方式对用户请求进行处理.无论以何种方式,它的基本原理是这样的.由客户端提出请求,请求某一页面 -----> WEB服务器引入指定相应脚本进行处理 -----> 脚本被载入服务器 -----> 由服务器指定的PHP解析器对脚本进行解析形成HTML语言形式 ----> 将解析后的HTML语句以包的方式传回给浏览器.由此不难看出,在页面发送到浏览器后,P

asp.net生成静态页面方法详细说明

最常用的方法从文件读取模版,替换模版中的参数后输出文件,这种方法的生成速度上比第一种要快许多,而且模版内容可以用工具任意编辑 主要代码: using system; using system.collections; using system.componentmodel; using system.data; using system.drawing; using system.web; using system.web.sessionstate; using system.web.ui; u