Simple ASP. NET programming method for generating static pages [demo source code download] and demo source code download
This example describes how to generate a static page by using ASP. NET. We will share this with you for your reference. The details are as follows:
1. Use Cases
When the page data does not need to be changed frequently, the static page mode can be used.
2. Benefits of using static pages
(1) Improve Website access speed
(2) reduce server burden
(3) Search Engine crawling
3. ASP. NET generate static pages
There are many ways to generate static pages. First, let's talk about one of the methods I use. References
Basic Ideas:
(1)create A template.html file, which defines some special string formats for content replacement, such as $ htmlformat
(2) read the template and assign values to the StringBuilder object.
(3) Replace the special string format with the desired content
(4) Create a New Static Page and write the StringBuilder object to the file.
4. Method
Using System; using System. collections. generic; using System. linq; using System. web; using System. text; using System. IO; /// <summary> /// ConvertHtmlPage to generate a static page // </summary> public class ConvertHtmlPage {// <summary> /// generate an HTML file /// </summary> /// <param name = "templatePath"> template path </param> /// <param name = "templateName"> Template name </param> /// <param name = "htmlPath"> Generate the HTML path </param> // <param name = "htmlName"> Generate HTML name </param> /// <param name = "format"> replaced content </param> /// <returns> </returns> public static bool CreatePage (string templatePath, string templateName, string htmlPath, string htmlName, List <string> format) {try {// read the template file StringBuilder htmltext = new StringBuilder (); using (StreamReader sr = new StreamReader (templatePath + templateName) {string line; while (line = sr. readLine ())! = Null) {htmltext. appendLine (line);} sr. close () ;}// Replace the TAG content in HTML for (int I = 0; I <format. count; I ++) {htmltext. replace ("$ htmlformat [" + I + "]", format [I]);} // generate the HTML file using (StreamWriter sw = new StreamWriter (htmlPath + htmlName, false, system. text. encoding. getEncoding ("GB2312") {sw. writeLine (htmltext); sw. flush (); sw. close () ;}} catch (Exception ex) {return false ;}return true ;}}
Appendix:Click here for the DEMO instanceDownload from this site.