Simple implementation of ASP. NET to generate static pages

Source: Internet
Author: User

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: generate a static page ///   </Summary>  Public   Class  Converthtmlpage {  ///   <Summary>      ///  Generate HTML files  ///   </Summary>      ///   <Param name = "templatepath">  Template path  </Param>      ///  <Param name = "templatename">  Template Name  </Param>      ///   <Param name = "htmlpath">  Generate 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 template files 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 HTML files              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  ;}} 

 

Download instance

 

Author: forevernome
Source: http://www.cnblogs.com/ForEvErNoME/
You are welcome to repost or share it, but be sure to declare it Article Source. If the article is helpful to you, I hope you can Recommendation Or Follow

     

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.