Asp.net static page generation method based on the form of replacement template page, asp.net Template
This example describes how to generate a static page in the form of a replacement template page in asp.net. We will share this with you for your reference. The details are as follows:
Step 1: Create a 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 ">
Step 2: 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 3: Compile static page generation 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 # Read the number of nodes in 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) {stri Ng 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 return nodeList set of the corresponding node. count ;}catch (Exception) {throw ;}} # endregion # region # create a folder /// <summary> /// create a folder /// </summary> /// <param name = "path"> path </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"> Configure file Path </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 the file name string absoCrePath = string. empty; // absolute path of the generated page string absoTemPath = string. empty; // absolute diameter int nodeCount = 0 on the template page; // 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 (the name can be changed here) 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.jb51.net"; arr [3] = DateTime. today. toString (); arr [4] = "Welcome to "; CreateHtmlBLL. createHtmlBLL chb = new CreateHtmlBLL. createHtmlBLL (); chb. createHtml ("CreateHtml. config "," web/website "," Template/TemplatePage.htm ", arr," Porschev ") ;}catch (Exception ex) {throw ex ;}# endregion