- 积分
- 2131
- 威望
- 0
- 魅力
- 0
- 经验
- 0
- 热心度
- 0
- 注册时间
- 2005-11-9
- 最后登录
- 2018-4-10
- 主题
- 289
- 回帖
- 714
- 精华
- 2
- 阅读权限
- 90
TA的每日心情 | 慵懒 2017-1-2 15:59 |
---|
签到天数: 10 天 连续签到: 1 天 [LV.3]火元素使者
论坛嘉宾
黄帝
- 积分
- 2131
|
<b>以下是引用片段:</b><br /> <!--Main.Aspx--> <br /><%@ page language="C#" %> <br /><%@ import namespace=System.IO %> <br /><script runat="server"> <br />protected override void OnInit (EventArgs e) <br />{ <br /> int id; <br /> try <br /> { <br /> id = int.Parse (Request.QueryString["id"]); <br /> } <br /> catch <br /> { <br /> throw (new Exception ("页面没有指定id")); <br /> } <br /> <br /> string filename=Server.MapPath("statichtml_"+id+".html"); <br /> <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 /> <br /> <br /> //调用Main_Execute,并且获取其输出 <br /> StringWriter sw = new StringWriter (); <br /> Server.Execute ("Main_Execute.aspx", sw); <br /> <br /> string content = sw.ToString (); <br /> <br /> //输出到客户端 <br /> Response.Write(content); <br /> Response.Flush(); <br /> <br /> //写进文件 <br /> <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>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 /></script> <br /><br /><!--Main_Execute.aspx--> <br /><%@ page language="C#" %> <br /><html> <br /><head runat="server"> <br /> <title>Untitled Page</title> <br /></head> <br /><body> <br />ID: <br /><%=Request.QueryString["id"]%> <br /></body> <br /></html> <br /> <!--Main.Aspx--> <br /><%@ page language="C#" %> <br /><%@ import namespace=System.IO %> <br /><script runat="server"> <br />protected override void OnInit (EventArgs e) <br />{ <br /> int id; <br /> try <br /> { <br /> id = int.Parse (Request.QueryString["id"]); <br /> } <br /> catch <br /> { <br /> throw (new Exception ("页面没有指定id")); <br /> } <br /> <br /> string filename=Server.MapPath("statichtml_"+id+".html"); <br /> <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 /> <br /> <br /> //调用Main_Execute,并且获取其输出 <br /> StringWriter sw = new StringWriter (); <br /> Server.Execute ("Main_Execute.aspx", sw); <br /> <br /> string content = sw.ToString (); <br /> <br /> //输出到客户端 <br /> Response.Write(content); <br /> Response.Flush(); <br /> <br /> //写进文件 <br /> <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>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 /></script> <br /><br /><!--Main_Execute.aspx--> <br /><%@ page language="C#" %> <br /><html> <br /><head runat="server"> <br /> <title>Untitled Page</title> <br /></head> <br /><body> <br />ID: <br /><%=Request.QueryString["id"]%> <br /></body> <br /></html> 其中原理是这样的。<br /> Main_Execute.aspx是生成HTML的页面。<br /> 现在用Main.aspx来对它进行缓存.<br /> 过程如下:<br /> 首先根据页面参数算出文件名。(这个例子只根据Request.QueryString["id"]来算)<br /> 尝试读取缓存的文件.如果成功,那么Response.End();<br /> 如果不成功:<br /> 使用Server.Execute来调用Main_Execute.aspx,并且获取它的结果内容。<br /> 得到内容后,立刻输出到客户端。<br /> 最后把内容写进文件里,提供给下一次做为缓存度取。 |
|