Simple ASP. NET programming method for generating static pages [demo source code download] and demo source code download

Source: Internet
Author: User

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.

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.