zoukankan      html  css  js  c++  java
  • asp.net生成静态页

    做个生成静态页示例:

     

    采用替换模版页的形式生成静态页————原文出处http://blog.csdn.net/porschev/article/details/6431646

     

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

     

     

    1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
    2. <html xmlns="http://www.w3.org/1999/xhtml">  
    3. <head>  
    4.     <title>Porschev 生成静态页简单示例</title>     
    5. </head>  
    6. <body>  
    7. <h1>$Porschev[0]$</h1>  
    8. <ul>  
    9. <li>页标题:$Porschev[0]$</li>  
    10. <li>名称:$Porschev[1]$</li>  
    11. <li>网址:<a href="$Porschev[2]$" mce_href="$Porschev[2]$" target="_blank">$Porschev[2]$</a></li>  
    12. <li>时间:$Porschev[3]$</li>  
    13. <li>详述:$Porschev[4]$</li>  
    14. </ul>  
    15. </body>  
    16. </html>  

     

     

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

     

     

    1. <?xml version="1.0" encoding="utf-8" ?>  
    2. <web>  
    3.   <website key="0" value="title"/>  
    4.   <website key="1" value="name"/>  
    5.   <website key="2" value="url"/>  
    6.   <website key="3" value="createDate"/>  
    7.   <website key="4" value="desc"/>  
    8. </web>  

     

     

    第三步:编写生成静态页代码:(添加System.Web引用)

     

     

    1. using System;  
    2. using System.Collections.Generic;  
    3. using System.Linq;  
    4. using System.Text;  
    5. using System.IO;  
    6. using System.Xml;  
    7. namespace CreateHtmlBLL  
    8. {  
    9.     public class CreateHtmlBLL  
    10.     {  
    11.         #region##读取配置文件某节点的个数  
    12.         /// <summary>  
    13.         /// 读取配置文件某节点的个数  
    14.         /// </summary>  
    15.         /// <param name="path">配置文件的路径</param>  
    16.         /// <param name="nodeName">要获取的节点</param>  
    17.         /// <returns>返回节点个数</returns>  
    18.         private int ReadConfig(string path,string nodeName)  
    19.         {  
    20.             string absoPath = string.Empty;  //绝对路径  
    21.             try  
    22.             {  
    23.                 absoPath = System.Web.HttpContext.Current.Server.MapPath(path);  
    24.                 XmlDocument xd = new XmlDocument();  
    25.                 xd.Load(absoPath);  
    26.                 XmlNodeList nodeList = xd.SelectNodes(nodeName);  //得到相应节点的集合  
    27.                 return nodeList.Count;  
    28.             }  
    29.             catch (Exception)  
    30.             {                  
    31.                 throw;  
    32.             }  
    33.         }  
    34.         #endregion  
    35.         #region##创建文件夹  
    36.         /// <summary>  
    37.         /// 创建文件夹  
    38.         /// </summary>  
    39.         /// <param name="path">要创建的路径</param>  
    40.         public void CreatFolder(string path)  
    41.         {  
    42.             string absoPath = string.Empty;  //绝对路径  
    43.             try  
    44.             {  
    45.                 absoPath = System.Web.HttpContext.Current.Server.MapPath(path);  
    46.                 if (!Directory.Exists(absoPath))  
    47.                 {  
    48.                     Directory.CreateDirectory(absoPath);  
    49.                 }  
    50.             }  
    51.             catch (Exception)  
    52.             {  
    53.                   
    54.                 throw;  
    55.             }  
    56.         }  
    57.         #endregion  
    58.  
    59.         #region##生成HTML页  
    60.         /// <summary>  
    61.         /// 生成HTML页  
    62.         /// </summary>  
    63.         /// <param name="configPath">配置文件的路径</param>  
    64.         /// <param name="configNodeName">配置文件节点名</param>  
    65.         /// <param name="temPath">模版页路径</param>  
    66.         /// <param name="arr">替换数组</param>  
    67.         /// <param name="createPath">生成HTML路径</param>  
    68.         public void CreateHtml(string configPath, String configNodeName, string temPath, string[] arr,string createPath)  
    69.         {            
    70.             string fileName = string.Empty;         //生成文件名  
    71.             string absoCrePath = string.Empty;      //生成页绝对路径  
    72.             string absoTemPath = string.Empty;      //模版页的绝对中径  
    73.             int nodeCount = 0;                      //节点数  
    74.              
    75.             try  
    76.             {  
    77.                 absoCrePath = System.Web.HttpContext.Current.Server.MapPath(createPath);  
    78.                 absoTemPath = System.Web.HttpContext.Current.Server.MapPath(temPath);  
    79.                 nodeCount = ReadConfig(configPath, configNodeName);  
    80.                 FileStream fs = File.Open(absoTemPath, FileMode.Open, FileAccess.Read);  //读取模版页  
    81.                 StreamReader sr = new StreamReader(fs, Encoding.GetEncoding("utf-8"));  
    82.                 StringBuilder sb = new StringBuilder(sr.ReadToEnd());  
    83.                 sr.Close();  
    84.                 sr.Dispose();  
    85.                 for (int i = 0; i < nodeCount; i++)  
    86.                 {  
    87.                     sb.Replace("$Porschev[" + i + "]$", arr[i]);  
    88.                 }  
    89.                 CreatFolder(createPath);  
    90.                 fileName = DateTime.Now.ToFileTime().ToString() + ".html";          //设置文件名(这里可以根据需要变化命名)  
    91.                 FileStream cfs = File.Create(absoCrePath + "/" + fileName);  
    92.                 StreamWriter sw = new StreamWriter(cfs, Encoding.GetEncoding("utf-8"));  
    93.                 sw.Write(sb.ToString());  
    94.                 sw.Flush();  
    95.                 sw.Close();  
    96.                 sw.Dispose();                 
    97.             }  
    98.             catch (Exception)  
    99.             {  
    100.                   
    101.                 throw;  
    102.             }                         
    103.         }  
    104.         #endregion  
    105.     }  
    106. }  

     

    第四步:测式生成

     

     

    1. protected void Page_Load(object sender, EventArgs e)  
    2.    {  
    3.        CreateHtml();  
    4.    }  
    5.    #region##生成静态页  
    6.    /// <summary>  
    7.    /// 生成静态页  
    8.    /// </summary>  
    9.    public void CreateHtml()  
    10.    {  
    11.        try  
    12.        {  
    13.            string[] arr = new string[5];  
    14.            arr[0] = "Porschev 静态页测式";  
    15.            arr[1] = "dtan";  
    16.            arr[2] = "www.dtan.so";  
    17.            arr[3] = DateTime.Today.ToString();  
    18.            arr[4] = "上班时间上CSDN,都是不好好工作的闲人。。。";  
    19.            CreateHtmlBLL.CreateHtmlBLL chb = new CreateHtmlBLL.CreateHtmlBLL();  
    20.            chb.CreateHtml("CreateHtml.config""web/website","Template/TemplatePage.htm", arr,"Porschev");  
    21.        }  
    22.        catch (Exception ex)  
    23.        {              
    24.            throw ex;  
    25.        }  
    26.    }  
    27.    #endregion  

     

     

     

     

    标例代码下载:http://download.csdn.net/source/3293015

     

  • 相关阅读:
    hyperledger fabric相关记录
    prometheus相关文章
    zookeeper和kafka的使用
    ubuntu1604 golang环境
    haproxy keepalived和nodejsv9的容器镜像所需文件
    图片相似算法
    oracle lpad rpad
    网站资料
    Win7 wifi热点设置
    Oracle存储过程编写经验和优化措施
  • 原文地址:https://www.cnblogs.com/wifi/p/2276436.html
Copyright © 2011-2022 走看看