ASP. NET static webpage Generation Method

Source: Internet
Author: User
Environment: Microsoft. NET Framework SDK V1.1
OS: Windows Server 2003 Chinese Version
ASP. NET generate static html pages
The FileSystemObject object used to generate static pages implemented in ASP!
System. Io is involved in. net.
Below is Program Code Note: This code is not original! Reference others' code
Code:
 // Generate an HTML page  Public   Static   Bool Writefile ( String Strtext, String Strcontent, String Strauthor ){String Path = httpcontext. Current. server. mappath (" /News/ "); Encoding code = encoding. getencoding (" Gb2312 "); // Read the Template File  String Temp = httpcontext. Current. server. mappath (" /News/text.html "); Streamreader sr = Null ; Streamwriter Sw = Null ; String STR ="  "; Try {Sr =New Streamreader (temp, Code); STR = Sr. readtoend (); // Read the file } Catch (Exception exp) {httpcontext. Current. response. Write (exp. Message); httpcontext. Current. response. End (); Sr. Close ();} String Htmlfilename = datetime. Now. tostring (" Yyyymmddhhmmss ") +" . Html "; // Replace content  // At this time, the template file has been read into the variable named STR STR = Str. Replace (" Showarticle ", Strtext ); // Showarticle on the template page STR = Str. Replace ("Biaoti ", Strtext); STR = Str. Replace (" Content ", Strcontent); STR = Str. Replace (" Author ", Strauthor ); // Write an object  Try {Sw = New Streamwriter (path + htmlfilename, False , Code); Sw. Write (STR); Sw. Flush ();} Catch (Exception ex) {httpcontext. Current. response. Write (ex. Message); httpcontext. Current. response. End ();} Finally {Sw. Close ();} Return   True ;}

This function is stored in the conn. CS base class.
Reference in the Code for adding news: Project name: hover

 
If(Hover. Conn. writefile (This. Title. Text. tostring (),This. Content. Text. tostring (),This. Author. Text. tostring () {response. Write ("Added");}Else{Response. Write ("An error occurred while generating HTML!");}

Template page text.html code
Code:

 < ! Doctype HTML public "-// W3C // dtd html 4.0 transitional // en" >  <  Html  >  < Head  >  <  Title  > Showarticle </  Title  >  <  Body  > Biaoti <  BR  > Content <  BR  > Author </  Body  >  </  Html  > Biaoti <  BR  > Content <  BR  > Author </  Body  >   </ Html  > 

A prompt is displayed, indicating that an HTML file with the current time as the file name is displayed! The above only writes the passed parameters to the HTML file. In actual applications, you need to add the database before writing the HTML file.

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.