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

本文來自:http://hi.baidu.com/tangdecai/blog/item/a7a02a7f66d6fb0e29388a13.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.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,並且獲取它的結果內容。

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

  最後把內容寫進文件裏,提供給下一次做爲緩存度取。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章