ASP.NET產生靜態頁面實現方法
<!--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 ( "頁面沒有指定id "));
}
string filename=Server.MapPath( "statichtml_ "+id+ ".html ");
//嘗試讀取已有檔案
Stream s = GetFileStream (filename);
if (s != null)//如果檔案存在並且讀取成功
{
using (s)
{
Stream2Stream (s, Response.OutputStream);
Response.End ();
}
}
//調用Main_Execute,並且擷取其輸出
StringWriter sw = new StringWriter ();
Server.Execute ( "Main_Execute.aspx ", sw);
string content = sw.ToString ();
//輸出到用戶端
Response.Write(content);
Response.Flush();
//寫進檔案
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; //1小時後到期
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 ( "頁面沒有指定id "));
}
string filename=Server.MapPath( "statichtml_ "+id+ ".html ");
//嘗試讀取已有檔案
Stream s = GetFileStream (filename);
if (s != null)//如果檔案存在並且讀取成功
{
using (s)
{
Stream2Stream (s, Response.OutputStream);
Response.End ();
}
}
//調用Main_Execute,並且擷取其輸出
StringWriter sw = new StringWriter ();
Server.Execute ( "Main_Execute.aspx ", sw);
string content = sw.ToString ();
//輸出到用戶端
Response.Write(content);
Response.Flush();
//寫進檔案
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; //1小時後到期
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_Execute.aspx是產生HTML的頁面。
現在用Main.aspx來對它進行緩衝.
過程如下:
首先根據頁面參數算出檔案名稱。(這個例子只根據Request.QueryString[ "id "]來算)
嘗試讀取緩衝的檔案.如果成功,那麼Response.End();
如果不成功:
使用Server.Execute來調用Main_Execute.aspx,並且擷取它的結果內容。
得到內容後,立刻輸出到用戶端。
最後把內容寫進檔案裡,提供給下一次做為緩衝度取。