Dynamic Web static: asp.net generate static HTML pages

Source: Internet
Author: User
Tags current time html page httpcontext tostring domain name
Asp.net| News | static | Web page

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

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.WriteFile (this. 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

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! 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

The following is a way for ASP to generate static Web pages

--------------------------------------------------------------------------------

The following code can help you generate static pages, such as: List.asp is read the database page, to be born in the list.htm static page, your domain name is xxx.com, you can use the following code, using the method:

If SaveFile ("/htm/list.htm", "http://www.xxx.com/asp/list.asp") Then
Response.Write "Generated"
Else
Response.Write "No Build"
End If

If the build fails, please block the code on error Resume next to see the specific error message

The code is as follows:

<%
If SaveFile ("/htm/list.htm", "http://www.xxx.com/asp/list.asp") Then
Response.Write "Generated"
Else
Response.Write "No Build"
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
%>



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.