Technique example: asp.net generate static page implementation method

Source: Internet
Author: User
Tags datetime flush tostring client
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&GT;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&GT;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.



Related Article

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.