Asp.net| Technique | static | page
<!--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 ("page does not specify ID"));
}
String Filename=server.mappath ("Statichtml_" +id+ ". html");
Attempt to read an existing file
Stream s = getfilestream (filename);
if (s!= null)//If the file exists and read successfully
{
using (s)
{
Stream2stream (S, Response.outputstream);
Response.End ();
}
}
Call Main_execute, and get its output
StringWriter SW = new StringWriter ();
Server.Execute ("main_execute.aspx", SW);
string content = sw. ToString ();
Output to Client
Response.Write (content);
Response.Flush ();
Write in 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[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; Expires after 1 hours
return new FileStream (filename, FileMode.Open, FileAccess.Read, FileShare.Read);
}
Catch
{
return null;
}
}
</script>
<!--main_execute.aspx-->
<%@ page language= "C #"%>
<title>untitled page</title>
<body>
Id:
<%=request.querystring["id"]%>
</body>
<!--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 ("page does not specify ID"));
}
String Filename=server.mappath ("Statichtml_" +id+ ". html");
Attempt to read an existing file
Stream s = getfilestream (filename);
if (s!= null)//If the file exists and read successfully
{
using (s)
{
Stream2stream (S, Response.outputstream);
Response.End ();
}
}
Call Main_execute, and get its output
StringWriter SW = new StringWriter ();
Server.Execute ("main_execute.aspx", SW);
string content = sw. ToString ();
Output to Client
Response.Write (content);
Response.Flush ();
Write in 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[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; Expires after 1 hours
return new FileStream (filename, FileMode.Open, FileAccess.Read, FileShare.Read);
}
Catch
{
return null;
}
}
</script>
<!--main_execute.aspx-->
<%@ page language= "C #"%>
<title>untitled page</title>
<body>
Id:
<%=request.querystring["id"]%>
</body>
The principle is this.
Main_execute.aspx is the page that generates HTML.
Now use main.aspx to cache it.
The process is as follows:
First, the file name is calculated based on the page parameters. (This example is only based on the request.querystring["id"].
An attempt was made to read a cached file. If successful, then Response.End ();
If not successful:
Use Server.Execute to invoke main_execute.aspx, and get its result content.
When the content is obtained, it is immediately exported to the client.
Finally, the content is written into the file, provided for the next time to do the cache size.