The example in this article describes how asp.net generates static pages based on alternate template pages. Share to everyone for your reference, specific as follows:
First step: Create a new project, creating 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 ">
Step Two: Create a 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 three: Write generated static page code: (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 # #读取配置文件某节点的个数///<summary>///Read configuration files Number of nodes///</summary>///<param name= "path" > configuration file Paths </param>///<param name= "NodeName" > To
Number of nodes </param>///<returns> 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 corresponding set of nodes return nodelist.count;
catch (Exception) {throw; }} #endregion #region # #创建文件夹///<summary>///Create folder///</summary>///<param NA Me= "path" > The path to be createdPath </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 # #生成HTML页///<summary>///generates an HTML page///</summary>///<para M name= "Configpath" > configuration file path </param>///<param name= "confignodename" > Profile section name </param>///<PA Ram Name= "Tempath" > Template page Path </param>///<param name= "arr" > Replacement array </param>///<param name= "Createp Ath "> Generate HTML path </param> public void createhtml (string configpath, String confignodename, String Tempath, String [] arr,string Createpath) {string fileName = string. Empty; Generate filename String absocrepath = string. Empty; Generate page Absolute path string Absotempath = string. Empty; Template page absolute medium diameter 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 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 file name (here can be renamed according to need) 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}}
Fourth step: The Measurement of generation
protected void Page_Load (object sender, EventArgs e)
{
createhtml ();
}
#region # #生成静态页
///<summary>
///generates 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.jb51.net";
ARR[3] = DateTime.Today.ToString ();
ARR[4] = "Welcome to the cloud-dwelling community";
CREATEHTMLBLL.CREATEHTMLBLL ChB = new Createhtmlbll.createhtmlbll ();
Chb. Createhtml ("Createhtml.config", "Web/website", "template/templatepage.htm", arr, "Porschev");
catch (Exception ex)
{
throw ex;
}
}
#endregion
More interested readers of asp.net related content can view the site topics: "asp.net operation JSON tips summary", "asp.net string operation tips Summary", "ASP.net Operation XML Skills summary", "asp.net file Operation skills Summary", " asp.net Ajax Skills Summary topic and the "ASP.net cache operation skills Summary."
I hope this article will help you to ASP.net program design.