top
Loading...
ASP.NET生成靜態頁面實現方法

以下是引用片段:

<%@ page language="C#" %>
<%@ import namespace=System.IO %>

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;
 }
}
 


<%@ page language="C#" %>


 Untitled Page


ID:
<%=Request.QueryString["id"]%>



<%@ page language="C#" %>
<%@ import namespace=System.IO %>

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;
 }
}
 


<%@ page language="C#" %>


 Untitled Page


ID:
<%=Request.QueryString["id"]%>

其中原理是這樣的。

Main_Execute.aspx是生成HTML的頁面。

現在用Main.aspx來對它進行緩存.

過程如下:

首先根據頁面參數算出文件名。(這個例子只根據Request.QueryString["id"]來算)

嘗試讀取緩存的文件.如果成功,那么Response.End();

如果不成功:

使用Server.Execute來調用Main_Execute.aspx,并且獲取它的結果內容。

得到內容后,立刻輸出到客戶端。

最后把內容寫進文件里,提供給下一次做為緩存度取。


作者:http://www.zhujiangroad.com
來源:http://www.zhujiangroad.com
北斗有巢氏 有巢氏北斗