Asp.Netはテンプレートページを置き換える形で静的ページを生成する

6901 ワード

ステップ1:プロジェクトを新規作成し、簡単なテンプレートページを作成します: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>

ステップ2: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>

ステップ3:静的ページコードの生成を記述する:(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
    }
}

ステップ4:測定生成
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.dtan.so";
            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