Alternative asp.net method for generating static pages

Source: Internet
Author: User

This article describes how to use the asp.net template replacement method to automatically generate html static pages. For more information, see.

Step 1: Create a project and create a simple template page: TemplatePage.htm

The Code is as follows: Copy code

<! 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> simple example of generating static pages by Porschev </title>
</Head>
<Body>
<H1> $ Porschev [0] $ <Ul>
<Li> page title: $ Porschev [0] $ </li>
<Li> name: $ Porschev [1] $ </li>
<Li> URL: <a href = "$ Porschev [2] $" target = "_ blank"> $ Porschev [2] $ </a> </li>
<Li> time: $ Porschev [3] $ </li>
<Li> details: $ Porschev [4] $ </li>
</Ul>
</Body>
</Html>

Step 2: Create a config File: CreateHtml. config

The Code is as follows: Copy code

<? 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: Compile static page generation Code: (add System. Web reference)

The Code is as follows: Copy code

Using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. IO;
Using System. Xml;
Namespace CreateHtmlBLL
{
Public class CreateHtmlBLL
{
# Region # number of nodes read from the configuration file
/// <Summary>
/// Read the number of nodes in the configuration file
/// </Summary>
/// <Param name = "path"> path of the configuration file </param>
/// <Param name = "nodeName"> node to be retrieved </param>
/// <Returns> Number of returned nodes </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 be created </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 an HTML page
/// <Summary>
/// Generate an HTML page
/// </Summary>
/// <Param name = "configPath"> path of the configuration file </param>
/// <Param name = "configNodeName"> Configuration File node name </param>
/// <Param name = "temPath"> template page path </param>
/// <Param name = "arr"> replace an array </param>
/// <Param name = "createPath"> Generate an HTML path </param>
Public void CreateHtml (string configPath, String configNodeName, string temPath, string [] arr, string createPath)
{
String fileName = string. Empty; // generate a file name
String absoCrePath = string. Empty; // the absolute path of the generated page.
String absoTemPath = string. Empty; // absolute path of the 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 the 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 (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

The Code is as follows: Copy code
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. bKjia. c0m ";
Arr [3] = DateTime. Today. ToString ();
Arr [4] = "CSDN during working hours is not a good job... ";
CreateHtmlBLL. CreateHtmlBLL chb = new CreateHtmlBLL. CreateHtmlBLL ();
Chb. CreateHtml ("CreateHtml. config", "web/website", "Template/TemplatePage.htm", arr, "Porschev ");
}
Catch (Exception ex)
{
Throw ex;
}
}
# Endregion

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.