Environment: Microsoft. NET Framework SDK V1.1
OS: Windows Server 2003 Chinese Version
ASP. NET generate static html pages
The FileSystemObject object used to generate static pages implemented in ASP!
System. Io is involved in. net.
The following is the program code Note: This code is not original! Reference others' code
Code:
// 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> |
For details, refer to the following:
Http://blog.csdn.net/lvmenglong/archive/2005/08/13/453617.aspx
Http://blog.csdn.net/gonaly/archive/2005/03/05/312071.aspx
Http://blog.csdn.net/cncca/archive/2005/06/22/400669.aspx
Http://blog.csdn.net/aoyux/archive/2004/10/14/135705.aspx