Asp tutorial x: three methods for generating static HTML pages
In the asp.net tutorial system, some dynamic pages are frequently used, such as our homepage index. aspx involves a lot of database tutorial queries. When there are users, the server constantly queries the database and actually does a lot of repetitive work.
MyPage. aspx on the server side
Customer terminal display mypage.htm
Client
To address this waste of resources, we are now designing a solution. First, we will not change the content for a period of time, but the static pages generated by a large number of dynamic pages will be stored on the server. When the client sends a request, let them directly generate the static page, as shown in the process.
Customer terminal display mypage.htm
Client
Execute
MyPage. aspx on the server side
Mypage.htm at the server end
Now we need a background program to complete these tasks.
We can make this a class classAspxToHtml and its code
Using System;
Using System. IO;
Using System. Web. UI;
Namespace LeoLu
{
/// Summary
/// AspxToHtml abstract description.
/// Summary
Public class AspxToHtml
{
/// Summary
/// Aspx File url
/// Summary
Public string strUrl;
/// Summary
/// Generate the html file storage path
/// Summary
Public string strSavePath;
/// Summary
/// Generate the html file name
/// Summary
Public string strSaveFile;
Public AspxToHtml ()
{
//
// Add the constructor logic here
//
}
/// Summary
/// Place the strUrl in the strSavePath directory and save it as strSaveFile
/// Summary
/// Whether returns is successful/returns
Public bool ExecAspxToHtml ()
{
Try
{
StringWriter strHTML = new StringWriter ();
System. Web. UI. Page myPage = new Page (); // There is a Server object in System. Web. UI. Page. We need to use it
MyPage. Server. Execute (strUrl, strHTML); // read the html content displayed in the client segment to strHTML.
StreamWriter sw = new StreamWriter (strSavePath + strSaveFile, true, System. Text. Encoding. GetEncoding ("GB2312 "));
// Create a new file test.htm in the format of GB2312
Sw. Write (strHTML. ToString (); // Write the contents in strhtmlto test.htm.
StrHTML. Close (); // Close StringWriter
Sw. Close (); // Close StreamWriter
Return true;
}
Catch
{
Return false;
}
}
/// Summary
/// Put the Url in the Path directory and save it as FileName
/// Summary
/// Param name = "Url" aspx page url/param
/// Param name = "Path" to generate the html file storage Path/param
/// Param name = "FileName" generate the html file name/param
/// Returns/returns
Public bool ExecAspxToHtml (string Url, string Path, string FileName)
{
Try
{
StringWriter strHTML = new StringWriter ();
System. Web. UI. Page myPage = new Page (); // There is a Server object in System. Web. UI. Page. We need to use it
MyPage. Server. Execute (Url, strHTML); // read the html content displayed in the client segment to strHTML.
StreamWriter sw = new StreamWriter (Path + FileName, true, System. Text. Encoding. GetEncoding ("GB2312 "));
// Create a new file test.htm in the format of GB2312
Sw. Write (strHTML. ToString (); // Write the contents in strhtmlto test.htm.
StrHTML. Close (); // Close StringWriter
Sw. Close (); // Close StreamWriter
Return true;
}
Catch
{
Return false;
}
}
}
}
Method:
Using System;
Using System. Data;
Using System. Configuration;
Using System. Collections;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Using System. IO;
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
Response. Write (AspxToHtml ("./admin/Default2.aspx", Server. MapPath ("./index.html ")));
}
/// <Summary>
/// Put the Url in the Path directory and save it as FileName
/// </Summary>
/// <Param name = "Url"> aspx page url </param>
/// <Param name = "PathFileName"> Save the path and generate an html file name </param>
/// <Returns> </returns>
Public bool AspxToHtml (string Url, string PathFileName)
{
Try
{
StringWriter strHTML = new StringWriter ();
System. Web. UI. Page myPage = new Page (); // There is a Server object in System. Web. UI. Page. We need to use it
MyPage. Server. Execute (Url, strHTML); // read the html content displayed in the client segment to strHTML.
// StreamWriter sw = new StreamWriter (PathFileName, false, System. Text. Encoding. GetEncoding ("GB2312 "));
StreamWriter sw = new StreamWriter (PathFileName, false, System. Text. Encoding. Default );
Sw. Write (strHTML. ToString (); // Write the characters in strHTML to the specified file.
StrHTML. Close ();
StrHTML. Dispose ();
Sw. Close ();
Sw. Dispose ();
Return true;
}
Catch
{
Return false;
}
}
}
Method B:
Using System;
Using System. Data;
Using System. Configuration;
Using System. Collections;
Using System. Web;
Using System. Web. Security;
Using System. Web. UI;
Using System. Web. UI. WebControls;
Using System. Web. UI. WebControls. WebParts;
Using System. Web. UI. HtmlControls;
Public partial class _ Default: System. Web. UI. Page
{
Protected void Page_Load (object sender, EventArgs e)
{
Label1.Text = DateTime. Now. ToString ();
}
Protected override void Render (HtmlTextWriter writer)
{
System. IO. StringWriter html = new System. IO. StringWriter ();
System. Web. UI. HtmlTextWriter tw = new System. Web. UI. HtmlTextWriter (html );
Base. Render (tw );
System. IO. StreamWriter sw;
Sw = new System. IO. StreamWriter (Server. MapPath ("Default.htm"), false, System. Text. Encoding. Default );
Sw. Write (html. ToString ());
Sw. Close ();
Tw. Close ();
Response. Write (html. ToString ());
}
}