Web page
The first is stencil replacement:
Environment: Microsoft. NET Framework SDK v1.1
Os:windows Server 2003 Chinese version
Asp. NET generates static HTML pages
The FileSystemObject object for generating static pages implemented in ASP!
This type of operation is involved in. NET System.IO
Here is the program code Note: This code is not original! Refer to other people's 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");
Reading template files
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 (); Reading files
}
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 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 ("Biaoti", StrText);
str = str. Replace ("Content", strcontent);
str = str. Replace ("Author", Strauthor);
Write a 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.
Reference note in 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 success");
}
Else
{
Response.Write ("Generate HTML Error!");
}
Template page text.html 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>
Tip Add a successful HTML file with the current time as the file name! 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 files.
The second type:
WebRequest accesses the ASPX page and then gets the response stream, which is the HTML
private void Button1_Click (object sender, System.EventArgs e)
{
Textbox1.text=this. Geturlvalue ("Http://YourUrl");
}
Use HttpWebRequest to get the return value of a URL
public string geturlvalue (string url)
{
System.Net.WebRequest httpwebrequest=system.net.webrequest.create (URL);
System.Net.WebResponse HttpWebResponse =httpwebrequest.getresponse ();
System.IO.StreamReader sr=new System.IO.StreamReader (Httpwebresponse.getresponsestream (), System.Text.Encoding.GetEncoding ("GB2312"));
return Sr. ReadToEnd ();
}