首页 > 编程 > .NET > 正文

技巧实例:ASP.NET生成静态页面实现方法

2024-07-10 13:08:22
字体:
来源:转载
供稿:网友

<!--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,并且获取它的结果内容。

  得到内容后,立刻输出到客户端。

  最后把内容写进文件里,提供给下一次做为缓存度取。

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表