ASP. NET static page generation Implementation Method
<! -- Main. aspx -->
<% @ Page Language = "C #" %>
<% @ Import namespace = system. Io %>
<SCRIPT runat = "server">
Protected override void oninit (eventargs E)
{
Int ID;
Try
{
Id = int. parse (request. querystring ["ID"]);
}
Catch
{
Throw (new exception ("No ID specified on the page "));
}
String filename = server. mappath ("statichtml _" + ID + ". html ");
// Try to read an existing file
Stream S = getfilestream (filename );
If (s! = NULL) // if the file exists and is read successfully
{
Using (s)
{
Stream2stream (S, response. outputstream );
Response. End ();
}
}
// Call main_execute and obtain its output
Stringwriter Sw = new stringwriter ();
Server. Execute ("main_execute.aspx", SW );
String content = Sw. tostring ();
// Output to the client
Response. Write (content );
Response. Flush ();
// Write the file
Try
{
Using (filestream FS = new filestream (filename, filemode. Create, fileaccess. Write, fileshare. Write ))
{
Using (streamwriter = new streamwriter (FS, response. contentencoding ))
{
Streamwriter. Write (content );
}
}
}
Finally
{
// Response. End ();
}
}
Static public void stream2stream (Stream SRC, stream DST)
{
Byte [] Buf = new byte [1, 4096];
While (true)
{
Int c = SRC. Read (BUF, 0, Buf. Length );
If (C = 0)
Return;
DST. Write (BUF, 0, C );
}
}
Public stream getfilestream (string filename)
{
Try
{
Datetime dt = file. getlastwritetime (filename );
Timespan Ts = DT-datetime. now;
If (TS. totalhours> 1)
Return NULL; // expired after 1 hour
Return new filestream (filename, filemode. Open, fileaccess. Read, fileshare. Read );
}
Catch
{
Return NULL;
}
}
</SCRIPT>
<! -- Main_execute.aspx -->
<% @ Page Language = "C #" %>
<HTML>
<Head runat = "server">
<Title> untitled page </title>
</Head>
<Body>
ID:
<% = Request. querystring ["ID"] %>
</Body>
</Html>
<! -- Main. aspx -->
<% @ Page Language = "C #" %>
<% @ Import namespace = system. Io %>
<SCRIPT runat = "server">
Protected override void oninit (eventargs E)
{
Int ID;
Try
{
Id = int. parse (request. querystring ["ID"]);
}
Catch
{
Throw (new exception ("No ID specified on the page "));
}
String filename = server. mappath ("statichtml _" + ID + ". html ");
// Try to read an existing file
Stream S = getfilestream (filename );
If (s! = NULL) // if the file exists and is read successfully
{
Using (s)
{
Stream2stream (S, response. outputstream );
Response. End ();
}
}
// Call main_execute and obtain its output
Stringwriter Sw = new stringwriter ();
Server. Execute ("main_execute.aspx", SW );
String content = Sw. tostring ();
// Output to the client
Response. Write (content );
Response. Flush ();
// Write the file
Try
{
Using (filestream FS = new filestream (filename, filemode. Create, fileaccess. Write, fileshare. Write ))
{
Using (streamwriter = new streamwriter (FS, response. contentencoding ))
{
Streamwriter. Write (content );
}
}
}
Finally
{
// Response. End ();
}
}
Static public void stream2stream (Stream SRC, stream DST)
{
Byte [] Buf = new byte [1, 4096];
While (true)
{
Int c = SRC. Read (BUF, 0, Buf. Length );
If (C = 0)
Return;
DST. Write (BUF, 0, C );
}
}
Public stream getfilestream (string filename)
{
Try
{
Datetime dt = file. getlastwritetime (filename );
Timespan Ts = DT-datetime. now;
If (TS. totalhours> 1)
Return NULL; // expired after 1 hour
Return new filestream (filename, filemode. Open, fileaccess. Read, fileshare. Read );
}
Catch
{
Return NULL;
}
}
</SCRIPT>
<! -- Main_execute.aspx -->
<% @ Page Language = "C #" %>
<HTML>
<Head runat = "server">
<Title> untitled page </title>
</Head>
<Body>
ID:
<% = Request. querystring ["ID"] %>
</Body>
</Html>
The principle is as follows.
Main_execute.aspx is the HTML page generated.
Now we use main. aspx to cache it.
The process is as follows:
First, calculate the file name based on the page parameters. (This example is based on request. querystring ["ID)
Try to read the cached file. If successful, then response. End ();
If not:
Use server. Execute to call main_execute.aspx and obtain its results.
After the content is obtained, it is immediately output to the client.
Finally, write the content into the file and provide it to the next cache.