How does Asp.net generate HTML?

Source: Internet
Author: User
// 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. writefilethis. Title. Text. tostring), this. content. Text. tostring), this. Author. Text. tostring )))
{
Response. Write ("added successfully ");
}
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.

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.