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 in ASP!
in.. Net involves system. io
The following is the Program Code . Note: This code is not original! Reference other people's Code
code:
// generate the HTML page
Public static bool writefile (string strtext, string strcontent, string strauthor)
{< br> 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
{< br> sr = new streamreader (temp, Code);
STR = sr. readtoend (); // read the file
}< br> catch (exception exp)
{< br> 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. writefile (this. 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.
A method for generating static webpages
The following code can help you generate static pages. For example, list.aspis the page for reading the data library, which is generated on the list.htm static page. your domain name is xxx.com. You can use the following code to use it:
If SaveFile ("/htm/list.htm", "http://www.xxx.com/asp/list.asp") then
Response. Write "generated"
Else
Response. Write "not generated"
End if
If generation fails, block the code on error resume next to view the specific error information.
The Code is as follows:
<%
If SaveFile ("/htm/list.htm", "http://www.xxx.com/asp/list.asp") Then
response. write "generated"
else
response. write "not generated"
end if
function SaveFile (localfilename, remotefileurl)
dim ads, retrieval, getremotedata
on error resume next
set retrieval = server. createobject ("microso" & "ft. XM "&" lhttp ")
with retrieval
. open "get", remotefileurl, false, "", ""
. send
getremotedata =. responsebody
end with
set retrieval = nothing
set ads = server. createobject ("Ado" & "DB. str "&" EAM ")
with ads
. type = 1
. open
. write getremotedata
. savetofile server. mappath (localfilename), 2
. cancel ()
. close ()
end with
set ads = nothing
If err <> 0 then
SaveFile = false
err. clear
else
SaveFile = true
end if
end function
%>