Implement static page (HTML) under ASP.net

Source: Internet
Author: User
Tags define file url html page html tags httpcontext implement tostring
asp.net| Static | page

When our web site traffic is very large, the client's every post to a large number of calls to the database server is a terrible thing. System performance can be greatly compromised, the speed is slow, the database is locked, and the heavy systems crash. This article will solve this problem by implementing a static HTML page.
1, the establishment of Conn.cs class documents
Using System;
Remember to add the following three references
Using System.Text;
Using System.Web;
Using System.IO;
Namespace Myservers
{
<summary>
Summary description of the Conn.
</summary>
public class Conn
{
Public Conn ()
{
//
TODO: Add constructor logic here
//
}
public bool WriteFile (string strtext,string strcontent,string strauthor)
{
String path = HttpContext.Current.Server.MapPath ("/myservers/news/");//define HTML File Store path
Encoding code = encoding.getencoding ("gb2312");//define Text encoding
Reading template files
String temp = HttpContext.Current.Server.MapPath ("/myservers/text.html");
StreamReader Sr=null;
StreamWriter Sw=null;
String Str= "";
Try
{
sr = new StreamReader (temp, code);
str = Sr. ReadToEnd (); Reading files
}
catch (Exception exp)
{
HttpContext.Current.Response.Write (exp. message);
HttpContext.Current.Response.End ();
Sr. Close ();
}
String Htmlfilename=path + DateTime.Now.ToString ("YYYYMMDDHHMMSS") + ". html";
Replace content
At this point, the template file has been read into a variable named str.
str = str. Replace ("Showarticle", StrText); Showarticle in the template page
str = str. Replace ("title", StrText);
str = str. Replace ("Content", strcontent);
str = str. Replace ("Author", Strauthor);
Write a file
Try
{
SW = new StreamWriter (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;
}
}
}
2, addnews.aspx documents
Add three and TextBox respectively: Tbx_title, Tbx_content, Tbx_author and one button:btn_addnews.
AddNews.aspx.cs file
private void Btn_addnews_click (object sender, System.EventArgs e)
{
Conn Hover = new Conn ();
if (Hover.writefile (this.txb_Title.Text.ToString), Server.htmldecode (This.txb_Content.Value), this.txb_ Author.Text.ToString ()))
{
Response.Write ("Add success");
}
Else
{
Response.Write ("Generate HTML Error!");
}
}
3, add template text.html file
<body>
Title<br>
Content<br>
Author
</body>
Description: The news folder must give the ASP.net user permission to write. This is a simple implementation example, the actual project must first save the data under the database, in Datagird called the database under the HTML file URL address.


Comments
# Re:asp.net to implement static page (HTML) 2005-09-12 23:52 sunshine
Note: By default, we cannot add HTML syntax to the TextBox or textarea, we must modify the config file and add <pages validaterequest= "false" under <system.web> />, but if you do this, you will be allowed to type HTML tags throughout your project, and you don't know other methods for the time being.
You must use the Server.htmldecode (this. Content.value). ToString () Decoding of characters!!!



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.