teach you to use ASP.net to generate static pages

Source: Internet
Author: User
Tags current time html page httpcontext tostring
asp.net| static Environment: Microsoft. NET Framework SDK v1.1 os:windows Server 2003 Chinese version asp.net generating static HTML pages used in generating static pages implemented in ASP Filesystemob Ject Object!

In. NET involves this kind of operation is System.IO the following is the code note: This code is not original! Refer to someone else's code

code://Generate 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 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 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. Replace ("Showarticle", strText);//showarticle str = str in the template page. Replace ("Biaoti", strText); str = str. Replace ("Content", strcontent); str = str. Replace ("Author", strauthor);/write file 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 placed in the Conn.CS base class to reference the note in the code to add News: project name is hover

if (Hover.Conn.WriteFilethis.Title.Text.ToString), this.Content.Text.ToString), this.Author.Text.ToString))

{Response.Write ("Add succeeded");} else {response.write ("Generate HTML Error!") ");}

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& lt;br> author </body> </HTML>

Tip Add a successful HTML file with the current time as the filename! The above is just the passing of several parameters directly into the HTML file, in the actual application of the need to add a database, and then write HTML file



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.