There are many ways to convert dynamic pages to static pages in ASP. NET. Based on actual needs, I found some Source code And make modifications. Now Code And instructions.
One is a page conversion class, which is implemented through the static function changfile (), and the conversion from dynamic pages to static pages.
Using system;
Using system. Data;
Using system. configuration;
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. text;
Using system. IO;
/** //
// summary description for htmlproxy
///
public class htmlproxy
... {
Public htmlproxy ()
... {
}< br> Public static bool changefile (int id)
... {
string filename = httpcontext. current. server. mappath ("post _" + ID + ". html ");
// try to read an existing file
stream ST = getfilestream (filename );
// if the file exists and is read successfully
If (st! = NULL)
... {
using (ST)
... {
streamtostream (St, httpcontext. current. response. outputstream);
return true;
// response. end ();
}< BR >}< br> else
... {
stringwriter Sw = new stringwriter ();
httpcontext. current. server. execute ("forumdetail. aspx? PID = "+ id, SW);
string content = Sw. tostring ();
// write it into the file
try
... {
using (filestream FS = new filestream (filename, filemode. create, fileaccess. write, fileshare. write)
... {
using (streamwriter STW = new streamwriter (FS, httpcontext. current. response. contentencoding)
... {
STW. write (content);
}< BR >}< br> return true;
}< br> catch... {return false ;}< BR >}
Private Static stream getfilestream (string filename)
... {
try
... {
datetime dt = file. getlastwritetime (filename);
timespan Ts = DT-datetime. now;
If (TS. totalhours> 1)
... {
// expired after one hour
return NULL;
}< br> return New filestream (filename, filemode. open, fileaccess. read, fileshare. read);
}< br> catch... {return NULL ;}< BR >}< br> static public void streamtostream (Stream SRC, stream DST)
... {
byte [] Buf = new byte [4096];
while (true)
... {
int c = SRC. read (BUF, 0, Buf. length);
If (C = 0)
return;
DST. write (BUF, 0, c);
}< BR >}< br> in the page file, forurl. the background code of aspx is as follows:
protected void page_load (Object sender, eventargs e)
... {
try
... {
int id = int. parse (request. querystring ["PID"]);
If (htmlproxy. changefile (ID)
... { response. redirect ("post _" + ID + ". html ");
}< br> else
... {
response. redirect ("post. aspx? PID = "+ id);
}< BR >}< br> catch... {
}< BR >}