asp.net| Static
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
The following is the program codeNote: This code is not original! Refer to someone else's code
Generate HTML page
Public static BOOLWriteFile (stringStrText,stringStrcontent,stringStrauthor)
{
stringPath = HttpContext.Current.Server.MapPath ("/news/");
Encoding code = encoding.getencoding ("gb2312");
Reading template files
stringtemp = HttpContext.Current.Server.MapPath ("/news/text.html");
StreamReader sr=NULL;
StreamWriter sw=NULL;
String Str= "";
Try
{
SR =NewStreamReader (temp, code);
str = Sr. ReadToEnd (); Reading files
}
Catch(Exception exp)
{
HttpContext.Current.Response.Write (exp. message);
HttpContext.Current.Response.End ();
Sr. Close ();
}
stringHtmlfilename=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>
Biaoti
<br>
Content<br>
Author
</body>
</HTML>
------------------------------------------------------------------------
Tip Add a successful HTML file with the current time as the file name! It's just a few of the parameters passed in directly to the HTML file, in the actual application need to add a database, and then write HTML file
And you need to write the generated file name and so on, and so on, this instance is only implemented based on the submission of parameters to replace the corresponding fields in the template! Need to perfect a lot of places! Who has the opinion, welcome to enlighten!