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
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
-------------------------------------------------------------------------
Biaoti
Content
Author
------------------------------------------------------------------------
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!
Posted on 2004-03-23 15:39 Free College Reading (2111) Comments (5) Edit collection to 365Key categories: dotnet
Feedback
# re:asp. NET generates static HTML pages! 2004-05-17 09:37 Kriss
With HTML as a template, replace the variables with only a few simple pages
Is there a way to generate HTML for the results of my aspx files?
such as my home page, the content is very complex, written in ASPX
But because a lot of users, in order to improve the speed, I update the data every time, the first page of the HTML, but the template is the aspx file reply
# re:asp. NET generates static HTML pages! 2004-05-18 18:03 the Venerable master
This effect can be implemented with caching, such as you will be the overall caching of the page 3 minutes, and static page almost reply
# re:asp. NET generates static HTML pages! 2004-06-01 13:36 Kriss
I found the way I wanted to use it:
Using System.Web.Hosting;
Webhost host = (webhost) applicationhost.createapplicationhost (
typeof (Webhost), name, path);
public void DoRequest (String page, string query, TextWriter writer) {
Httpruntime.processrequest (New SimpleWorkerRequest (page, query, writer));
}
So I can create an HTML file directly from an ASPX file.
FileStream fs = File.Open ("default.htm");
Host. DoRequest ("default.aspx", string. EMPTY,FS); Reply