佳丽云

英雄无敌3-WoG中文站

 找回密码
 英雄注册|Register
查看: 2481|回复: 0

ASP.NET生成静态页面实现方法

[复制链接]
  • TA的每日心情
    慵懒
    2017-1-2 15:59
  • 签到天数: 10 天

    连续签到: 1 天

    [LV.3]火元素使者

    714

    回帖

    6万

    金币

    2

    精华

    论坛嘉宾

    黄帝

    Rank: 9Rank: 9Rank: 9

    积分
    2131

    退役版主社区QQ达人

    QQ
    发表于 2007-6-29 22:25:24 | 显示全部楼层 |阅读模式
    <b>以下是引用片段:</b><br />  &lt;!--Main.Aspx--&gt; <br />&lt;%@ page language=&quot;C#&quot; %&gt; <br />&lt;%@ import namespace=System.IO %&gt; <br />&lt;script runat=&quot;server&quot;&gt; <br />protected override void OnInit (EventArgs e) <br />{ <br />  int id; <br />  try <br />  { <br />    id = int.Parse (Request.QueryString[&quot;id&quot;]); <br />  } <br />  catch <br />  { <br />    throw (new Exception (&quot;页面没有指定id&quot;)); <br />  } <br /> &nbsp;&nbsp;<br />  string filename=Server.MapPath(&quot;statichtml_&quot;+id+&quot;.html&quot;); <br /> &nbsp;&nbsp;<br />  //尝试读取已有文件 <br />  Stream s = GetFileStream (filename); <br />  if (s != null)//如果文件存在并且读取成功 <br />  { <br />    using (s) <br />    { <br />      Stream2Stream (s, Response.OutputStream); <br />      Response.End (); <br />    } <br />  } <br /> &nbsp;&nbsp;<br /> &nbsp;&nbsp;<br />  //调用Main_Execute,并且获取其输出 <br />  StringWriter sw = new StringWriter (); <br />  Server.Execute (&quot;Main_Execute.aspx&quot;, sw); <br /> &nbsp;&nbsp;<br />  string content = sw.ToString (); <br /> &nbsp;&nbsp;<br />  //输出到客户端 <br />  Response.Write(content); <br />  Response.Flush(); <br /> &nbsp;&nbsp;<br />  //写进文件 <br /> &nbsp;&nbsp;<br />  try <br />  { <br />    using (FileStream fs = new FileStream (filename, FileMode.Create, FileAccess.Write, FileShare.Write)) <br />    { <br />      using (StreamWriter streamwriter = new StreamWriter (fs, Response.ContentEncoding)) <br />      { <br />        streamwriter.Write (content); <br />      } <br />    } <br />  } <br />  finally <br />  { <br />    //Response.End (); <br />  } <br />} <br />static public void Stream2Stream (Stream src, Stream dst) <br />{ <br />  byte[] buf = new byte[4096]; <br />  while (true) <br />  { <br />    int c = src.Read (buf, 0, buf.Length); <br />    if(c==0) <br />      return; <br />    dst.Write (buf, 0, c); <br />  } <br />} <br />public Stream GetFileStream(string filename) <br />{ <br />  try <br />  { <br />    DateTime dt = File.GetLastWriteTime (filename); <br />    TimeSpan ts=dt - DateTime.Now; <br />    if(ts.TotalHours&gt;1) <br />      return null;    //1小时后过期 <br />    return new FileStream (filename, FileMode.Open, FileAccess.Read, FileShare.Read); <br />  } <br />  catch <br />  { <br />    return null; <br />  } <br />} <br />&lt;/script&gt;&nbsp;&nbsp;<br /><br />&lt;!--Main_Execute.aspx--&gt; <br />&lt;%@ page language=&quot;C#&quot; %&gt; <br />&lt;html&gt; <br />&lt;head runat=&quot;server&quot;&gt; <br />  &lt;title&gt;Untitled Page&lt;/title&gt; <br />&lt;/head&gt; <br />&lt;body&gt; <br />ID: <br />&lt;%=Request.QueryString[&quot;id&quot;]%&gt; <br />&lt;/body&gt; <br />&lt;/html&gt; <br />  &lt;!--Main.Aspx--&gt; <br />&lt;%@ page language=&quot;C#&quot; %&gt; <br />&lt;%@ import namespace=System.IO %&gt; <br />&lt;script runat=&quot;server&quot;&gt; <br />protected override void OnInit (EventArgs e) <br />{ <br />  int id; <br />  try <br />  { <br />    id = int.Parse (Request.QueryString[&quot;id&quot;]); <br />  } <br />  catch <br />  { <br />    throw (new Exception (&quot;页面没有指定id&quot;)); <br />  } <br /> &nbsp;&nbsp;<br />  string filename=Server.MapPath(&quot;statichtml_&quot;+id+&quot;.html&quot;); <br /> &nbsp;&nbsp;<br />  //尝试读取已有文件 <br />  Stream s = GetFileStream (filename); <br />  if (s != null)//如果文件存在并且读取成功 <br />  { <br />    using (s) <br />    { <br />      Stream2Stream (s, Response.OutputStream); <br />      Response.End (); <br />    } <br />  } <br /> &nbsp;&nbsp;<br /> &nbsp;&nbsp;<br />  //调用Main_Execute,并且获取其输出 <br />  StringWriter sw = new StringWriter (); <br />  Server.Execute (&quot;Main_Execute.aspx&quot;, sw); <br /> &nbsp;&nbsp;<br />  string content = sw.ToString (); <br /> &nbsp;&nbsp;<br />  //输出到客户端 <br />  Response.Write(content); <br />  Response.Flush(); <br /> &nbsp;&nbsp;<br />  //写进文件 <br /> &nbsp;&nbsp;<br />  try <br />  { <br />    using (FileStream fs = new FileStream (filename, FileMode.Create, FileAccess.Write, FileShare.Write)) <br />    { <br />      using (StreamWriter streamwriter = new StreamWriter (fs, Response.ContentEncoding)) <br />      { <br />        streamwriter.Write (content); <br />      } <br />    } <br />  } <br />  finally <br />  { <br />    //Response.End (); <br />  } <br />} <br />static public void Stream2Stream (Stream src, Stream dst) <br />{ <br />  byte[] buf = new byte[4096]; <br />  while (true) <br />  { <br />    int c = src.Read (buf, 0, buf.Length); <br />    if(c==0) <br />      return; <br />    dst.Write (buf, 0, c); <br />  } <br />} <br />public Stream GetFileStream(string filename) <br />{ <br />  try <br />  { <br />    DateTime dt = File.GetLastWriteTime (filename); <br />    TimeSpan ts=dt - DateTime.Now; <br />    if(ts.TotalHours&gt;1) <br />      return null;    //1小时后过期 <br />    return new FileStream (filename, FileMode.Open, FileAccess.Read, FileShare.Read); <br />  } <br />  catch <br />  { <br />    return null; <br />  } <br />} <br />&lt;/script&gt;&nbsp;&nbsp;<br /><br />&lt;!--Main_Execute.aspx--&gt; <br />&lt;%@ page language=&quot;C#&quot; %&gt; <br />&lt;html&gt; <br />&lt;head runat=&quot;server&quot;&gt; <br />  &lt;title&gt;Untitled Page&lt;/title&gt; <br />&lt;/head&gt; <br />&lt;body&gt; <br />ID: <br />&lt;%=Request.QueryString[&quot;id&quot;]%&gt; <br />&lt;/body&gt; <br />&lt;/html&gt;  其中原理是这样的。<br />  Main_Execute.aspx是生成HTML的页面。<br />  现在用Main.aspx来对它进行缓存.<br />  过程如下:<br />  首先根据页面参数算出文件名。(这个例子只根据Request.QueryString[&quot;id&quot;]来算)<br />  尝试读取缓存的文件.如果成功,那么Response.End();<br />  如果不成功:<br />  使用Server.Execute来调用Main_Execute.aspx,并且获取它的结果内容。<br />  得到内容后,立刻输出到客户端。<br />  最后把内容写进文件里,提供给下一次做为缓存度取。
    ahome_bigavatar:guest
    ahome_bigavatar:welcomelogin
    您需要登录后才可以回帖 登录 | 英雄注册|Register

    本版积分规则

    捐赠
    关注我们,英3Mod一网打尽!

    WoG中文站 ( 辽B2-20210485-10 )|辽公网安备 21128202000228 号

    GMT+8, 2024-11-15 10:37 , Processed in 0.260050 second(s), 14 queries , Gzip On, File On.

    Powered by Discuz! X3.4

    Copyright © 2004-2022, Beijing Second Sight Technology Co., LTD.

    快速回复 返回顶部 返回列表