ASP.NET页面如何建立静态缓存

方法A:使用 HttpModule 技术拦截页面访问,导向静态缓存页

步骤:

1、创建一个新的HtppModule,拦截对aspx类型页面的访问,判断是否有静态缓存页

IHttpModule 全局拦截代码
 1 using System;
 2 using System.IO;
 3 using System.Data;
 4 using System.Configuration;
 5 using System.Web;
 6 using System.Web.Security;
 7 using System.Web.UI;
 8 using System.Web.UI.WebControls;
 9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11 
12 /// <summary>
13 /// StaticFileCacheModule 的摘要说明
14 /// </summary>
15 public class StaticFileCacheModule:IHttpModule
16 {
17     public void Init(HttpApplication context)
18     {
19         context.BeginRequest += new EventHandler(context_BeginRequest);
20     }
21     void context_BeginRequest(object sender, EventArgs e)
22     {
23         HttpContext context = ((HttpApplication)sender).Context;
24         //判断是否需要处理
25         if (context.Request.AppRelativeCurrentExecutionFilePath.ToLower().EndsWith(".aspx"))
26         {
27             string fileUrl = "~/CacheFile/"+ GetFileName(context);
28             if (File.Exists(context.Server.MapPath(fileUrl)))
29             {
30                 //如果静态缓存文件存在,直接返回缓存文件
31                 context.RewritePath(fileUrl, false);
32             }
33         }
34     }
35     public static string GetFileName(HttpContext context)
36     {
37         //我们的缓存文件名由页面文件名加上查询字符串组成
38         return context.Request.AppRelativeCurrentExecutionFilePath
39                 .ToLower()
40                 .Replace(".aspx""")
41                 .Replace("~/","")
42                 .Replace("/","_")  +
43                context.Request.Url.Query
44                 .Replace("?","_")
45                 .Replace("&","_")+".html";
46         
47     }
48     public void Dispose() {}
49 }
50 

 此 HttpMudole 类文件放入 App_Code 目录

2、创建一个Page子类,重写Render方法,在其中将页面生成的最终结果保存在指定目录下

需要建立缓存的页面,继承此类即可。 

能够生成静态页面的Page子类代码
 1 using System;
 2 using System.IO;
 3 using System.Data;
 4 using System.Configuration;
 5 using System.Web;
 6 using System.Web.Security;
 7 using System.Web.UI;
 8 using System.Web.UI.WebControls;
 9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11 /// <summary>
12 /// StaticFileCachePageBase 的摘要说明
13 /// </summary>
14 public class StaticFileCachePageBase : System.Web.UI.Page 
15 {
16     protected string GetFileName()
17     {
18         //我们的缓存文件名由页面文件名加上查询字符串组成
19         return Request.AppRelativeCurrentExecutionFilePath
20                 .ToLower()
21                 .Replace(".aspx""")
22                 .Replace("~/""")
23                 .Replace("/""_"+
24                Request.Url.Query
25                 .Replace("?""_")
26                 .Replace("&""_"+ ".html";
27     }
28     protected override void Render(HtmlTextWriter writer)
29     {
30         StringWriter sw = new StringWriter();
31         HtmlTextWriter htmlw = new HtmlTextWriter(sw);
32         //调用Render方法,把页面内容输出到StringWriter中
33         base.Render(htmlw);
34         htmlw.Flush();
35         htmlw.Close();
36         //获得页面内容
37         string pageContent = sw.ToString();
38         string fullpath = Server.MapPath("~/CacheFile/")+ GetFileName();
39         string path = Path.GetDirectoryName(fullpath);
40         if (!Directory.Exists(path))
41         {
42             Directory.CreateDirectory(path);
43         }
44         //把页面内容保存到静态文件中
45         using (StreamWriter stringWriter = File.AppendText(fullpath))
46         {
47             stringWriter.Write(pageContent);
48         }
49         //将页面内容输出到浏览器
50         Response.Write(pageContent);
51     }
52 }

 方法B、不拦截所有的aspx页面,由具有静态缓存功能的网页自己判断是否该加载静态页

需要建立静态缓存的页面,将默认的继承 Page 修改为继承此 StaticFileCachePageBase 类即可

 (可以用在只有少数页面需要缓冲的情况下)

能生成静态缓存页并可自行判断缓存是否存在的Page子类
 1 using System;
 2 using System.IO;
 3 using System.Data;
 4 using System.Configuration;
 5 using System.Web;
 6 using System.Web.Security;
 7 using System.Web.UI;
 8 using System.Web.UI.WebControls;
 9 using System.Web.UI.WebControls.WebParts;
10 using System.Web.UI.HtmlControls;
11 /// <summary>
12 /// StaticFileCachePageBase 的摘要说明
13 /// </summary>
14 public class StaticFileCachePageBase : System.Web.UI.Page 
15 {
16     protected override void OnPreInit(EventArgs e)
17     {
18         string fileUrl = "~/CacheFile/" + GetFileName();
19         if (File.Exists(Server.MapPath(fileUrl)))
20         {
21             //如果静态缓存文件存在,直接返回缓存文件
22             Server.Transfer(fileUrl);
23         }
24         base.OnPreInit(e);
25     }
26     protected string GetFileName()
27     {
28         //我们的缓存文件名由页面文件名加上查询字符串组成
29         return Request.AppRelativeCurrentExecutionFilePath
30                 .ToLower()
31                 .Replace(".aspx""")
32                 .Replace("~/""")
33                 .Replace("/""_"+
34                Request.Url.Query
35                 .Replace("?""_")
36                 .Replace("&""_"+ ".html";
37     }
38     protected override void Render(HtmlTextWriter writer)
39     {
40         StringWriter sw = new StringWriter();
41         HtmlTextWriter htmlw = new HtmlTextWriter(sw);
42         //调用Render方法,把页面内容输出到StringWriter中
43         base.Render(htmlw);
44         htmlw.Flush();
45         htmlw.Close();
46         //获得页面内容
47         string pageContent = sw.ToString();
48         string fullpath = Server.MapPath("~/CacheFile/")+ GetFileName();
49         string path = Path.GetDirectoryName(fullpath);
50         if (!Directory.Exists(path))
51         {
52             Directory.CreateDirectory(path);
53         }
54         //把页面内容保存到静态文件中
55         using (StreamWriter stringWriter = File.AppendText(fullpath))
56         {
57             stringWriter.Write(pageContent);
58         }
59         //将页面内容输出到浏览器
60         Response.Write(pageContent);
61     }
62 }

  

关于缓存页面的失效问题:

目前我采用自建的任务管理器,自动定期删除静态的html网页

原文地址:https://www.cnblogs.com/chinaontology/p/aspCacheFile.html