Method 1: send a request to the dynamic page of the server to obtain the HTML of the pageCode. The disadvantage of this method is obvious: slow speed. In addition, if the requested dynamic page has a verification control, the returned HTML page cannot perform data verification. However, this method is relatively simple to write. The main code is as follows:
# Region // generate the static page of the requested URL
Public static void geturltohtml (string URL, string path) // The URL is the dynamic page address, and the path is the generated static page.
{
Try
{
System. net. webrequest wreq = system. net. webrequest. Create (URL );
// Get the response instance.
System. net. webresponse wresp = wreq. getresponse ();
// Get the response stream.
System. Io. Stream respstream = wresp. getresponsestream ();
// Dim reader as streamreader = new streamreader (respstream)
System. Io. streamreader reader = new system. Io. streamreader (respstream, system. Text. encoding. getencoding ("gb2312 "));
String STR = reader. readtoend ();
System. Io. streamwriter Sw = new system. Io. streamwriter (path, false, system. Text. encoding. getencoding ("gb2312 "));
Sw. Write (STR );
Sw. Flush ();
Sw. Close ();
System. Web. httpcontext. Current. response. Write ("<SCRIPT> alert ('page generation successful! '); </SCRIPT> ");
}
Catch (system. Exception ex)
{
System. Web. httpcontext. Current. response. Write ("<SCRIPT> alert ('page generation failed! "+ Ex. Message +" '); </SCRIPT> ");
}
}
# Endregion
Method 2: Read the template from the file, replace the parameters in the template, and output the file. This method is generated much faster than the first method, and the template content can be edited using tools.
Main Code:
using system;
using system. collections;
using system. componentmodel;
using system. data;
using system. drawing;
using system. web;
using system. web. sessionstate;
using system. web. ui;
using system. web. UI. webcontrols;
using system. web. UI. htmlcontrols;
using system. io;
using system. text;
Namespace Xinxi
{
/// <Summary>
/// Summary of createpage.
/// </Summary>
// Www.365xinxi.net
// This class is small for generating static webpagesProgram
Public class create
{
Public void createpage ()
{
}
Public static bool writefile (string strtext, string strcontent, string strauthor)
{
String Path = httpcontext. Current. server. mappath ("/test/"); // file output directory
Encoding code = encoding. getencoding ("gb2312 ");
// Read the Template File
String temp = httpcontext. Current. server. mappath ("/template/test.html"); // Template File
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"; // static file name
// 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;
}
}
}
// The principle is to use the class in system. Io to read and write the template file, replace the tag in the template with replace, and write the static html
method 3: if a large number of files are generated, the method 2 reads the template content repeatedly, in this case, you can use the third method -- to directly write your template in the code, which is similar to the method used to create the header and footer of the website I wrote last time:
using system;
using system. collections;
using system. data;
using system. data. oledb;
using system. text;
using system. io;
using system. web;
using system. web. ui;
using system. web. UI. webcontrols;
using system. web. UI. htmlcontrols;
Namespace Xinxi
{
/// <Summary>
/// Custom Public Functions
/// </Summary>
Public class myfun
{
# Region // definition template page
Public static string sitetemplate ()
{
String STR = "";
STR + = "..."; // template page HTML code
Return STR;
}
# Endregion
Public static bool writefile (string strtext, string strcontent, string strauthor)
{
String Path = httpcontext. Current. server. mappath ("/test/"); // file output directory
Encoding code = encoding. getencoding ("gb2312 ");
Streamwriter Sw = NULL;
String STR = sitetemplate (); // read the HTML code of the template page
String htmlfilename = datetime. Now. tostring ("yyyymmddhhmmss") + ". html"; // static file name
// Replace content
STR = Str. Replace ("showarticle", strtext );
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;
}
}
}
The generation speed is from slow to fast, and the ease of operation is from simple to complex. Select an appropriate method based on the actual situation.