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.