asp. net method for generating static pages based on the form of replacement template pages

  • 2021-08-05 09:32:12
  • OfStack

This article illustrates how asp. net generates static pages based on the form of replacement template pages. Share it for your reference, as follows:

Step 1: Create a new project and create a simple template page: 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  Simple example of building a static page </title>
</head>
<body>
<h1>$Porschev[0]$</h1>
<ul>
<li> Page title: $Porschev[0]$</li>
<li> Name: $Porschev[1]$</li>
<li> Website: <a href="$Porschev[2]$" target="_blank">$Porschev[2]$</a></li>
<li> Time: $Porschev[3]$</li>
<li> Detailed description: $Porschev[4]$</li>
</ul>
</body>
</html>

Step 2: Create an config file: 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>

Step 3: Write code to generate a static page: (Add System. Web reference)


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Xml;
namespace CreateHtmlBLL
{
  public class CreateHtmlBLL
  {
    #region## Read the number of nodes in the configuration file 
    ///<summary>
    ///  Read the number of nodes in the configuration file 
    ///</summary>
    ///<param name="path"> Path to the configuration file </param>
    ///<param name="nodeName"> Node to get </param>
    ///<returns> Number of nodes returned </returns>
    private int ReadConfig(string path,string nodeName)
    {
      string absoPath = string.Empty; // Absolute path 
      try
      {
        absoPath = System.Web.HttpContext.Current.Server.MapPath(path);
        XmlDocument xd = new XmlDocument();
        xd.Load(absoPath);
        XmlNodeList nodeList = xd.SelectNodes(nodeName); // Get the set of corresponding nodes 
        return nodeList.Count;
      }
      catch (Exception)
      {
        throw;
      }
    }
    #endregion
    #region## Create a folder 
    ///<summary>
    ///  Create a folder 
    ///</summary>
    ///<param name="path"> Path to create </param>
    public void CreatFolder(string path)
    {
      string absoPath = string.Empty; // Absolute path 
      try
      {
        absoPath = System.Web.HttpContext.Current.Server.MapPath(path);
        if (!Directory.Exists(absoPath))
        {
          Directory.CreateDirectory(absoPath);
        }
      }
      catch (Exception)
      {
        throw;
      }
    }
    #endregion
    #region## Generate HTML Page 
    ///<summary>
    ///  Generate HTML Page 
    ///</summary>
    ///<param name="configPath"> Path to the configuration file </param>
    ///<param name="configNodeName"> Configuration file node name </param>
    ///<param name="temPath"> Template page path </param>
    ///<param name="arr"> Replace array </param>
    ///<param name="createPath"> Generate HTML Path </param>
    public void CreateHtml(string configPath, String configNodeName, string temPath, string[] arr,string createPath)
    {
      string fileName = string.Empty;     // Generate file name 
      string absoCrePath = string.Empty;   // Generate page absolute path 
      string absoTemPath = string.Empty;   // Absolute median diameter of template page 
      int nodeCount = 0;           // Number of nodes 
      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); // Read a template page 
        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";     // Set the file name (here you can change the name as needed) 
        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
  }
}

Step 4: Test generation


protected void Page_Load(object sender, EventArgs e)
{
    CreateHtml();
}
#region## Generate a static page 
///<summary>
///  Generate a static page 
///</summary>
public void CreateHtml()
{
    try
    {
      string[] arr = new string[5];
      arr[0] = "Porschev  Static page test type ";
      arr[1] = "dtan";
      arr[2] = "www.ofstack.com";
      arr[3] = DateTime.Today.ToString();
      arr[4] = " Welcome to the House of Scripts ";
      CreateHtmlBLL.CreateHtmlBLL chb = new CreateHtmlBLL.CreateHtmlBLL();
      chb.CreateHtml("CreateHtml.config", "web/website","Template/TemplatePage.htm", arr,"Porschev");
    }
    catch (Exception ex)
    {
      throw ex;
    }
}
#endregion

For more readers interested in asp. net, please check the topics on this site: "Summary of asp. net Operation json Skills", "Summary of asp. net String Operation Skills", "Summary of XML Operation Skills", "Summary of asp. net File Operation Skills", "Summary of asp. net ajax Skills" and "Summary of asp. net Cache Operation Skills".

I hope this paper is helpful to everyone's asp. net programming.


Related articles: