ASP. NET static page generation Implementation Method

Source: Internet
Author: User

<! -- 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 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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.