方法A:使用 HttpModule 技术拦截页面访问,导向静态缓存页
步骤:
1、创建一个新的HtppModule,拦截对aspx类型页面的访问,判断是否有静态缓存页
using System; using System.IO; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; /// <summary> /// StaticFileCacheModule 的摘要说明 /// </summary> public class StaticFileCacheModule:IHttpModule { public void Init(HttpApplication context) { context.BeginRequest += new EventHandler(context_BeginRequest); } void context_BeginRequest(object sender, EventArgs e) { HttpContext context = ((HttpApplication)sender).Context; //判断是否需要处理 if (context.Request.AppRelativeCurrentExecutionFilePath.ToLower().EndsWith(".aspx")) { string fileUrl = "~/CacheFile/"+ GetFileName(context); if (File.Exists(context.Server.MapPath(fileUrl))) { //如果静态缓存文件存在,直接返回缓存文件 context.RewritePath(fileUrl, false); } } } public static string GetFileName(HttpContext context) { //我们的缓存文件名由页面文件名加上查询字符串组成 return context.Request.AppRelativeCurrentExecutionFilePath .ToLower() .Replace(".aspx", "") .Replace("~/","") .Replace("/","_") + context.Request.Url.Query .Replace("?","_") .Replace("&","_")+".html"; } public void Dispose() {} }
此 HttpMudole 类文件放入 App_Code 目录
2、创建一个Page子类,重写Render方法,在其中将页面生成的最终结果保存在指定目录下
需要建立缓存的页面,继承此类即可。
using System; using System.IO; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; /// <summary> /// StaticFileCachePageBase 的摘要说明 /// </summary> public class StaticFileCachePageBase : System.Web.UI.Page { protected string GetFileName() { //我们的缓存文件名由页面文件名加上查询字符串组成 return Request.AppRelativeCurrentExecutionFilePath .ToLower() .Replace(".aspx", "") .Replace("~/", "") .Replace("/", "_") + Request.Url.Query .Replace("?", "_") .Replace("&", "_") + ".html"; } protected override void Render(HtmlTextWriter writer) { StringWriter sw = new StringWriter(); HtmlTextWriter htmlw = new HtmlTextWriter(sw); //调用Render方法,把页面内容输出到StringWriter中 base.Render(htmlw); htmlw.Flush(); htmlw.Close(); //获得页面内容 string pageContent = sw.ToString(); string fullpath = Server.MapPath("~/CacheFile/")+ GetFileName(); string path = Path.GetDirectoryName(fullpath); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } //把页面内容保存到静态文件中 using (StreamWriter stringWriter = File.AppendText(fullpath)) { stringWriter.Write(pageContent); } //将页面内容输出到浏览器 Response.Write(pageContent); } }
方法B、不拦截所有的aspx页面,由具有静态缓存功能的网页自己判断是否该加载静态页
需要建立静态缓存的页面,将默认的继承 Page 修改为继承此 StaticFileCachePageBase 类即可
(可以用在只有少数页面需要缓冲的情况下)
using System; using System.IO; using System.Data; using System.Configuration; using System.Web; using System.Web.Security; using System.Web.UI; using System.Web.UI.WebControls; using System.Web.UI.WebControls.WebParts; using System.Web.UI.HtmlControls; /// <summary> /// StaticFileCachePageBase 的摘要说明 /// </summary> public class StaticFileCachePageBase : System.Web.UI.Page { protected override void OnPreInit(EventArgs e) { string fileUrl = "~/CacheFile/" + GetFileName(); if (File.Exists(Server.MapPath(fileUrl))) { //如果静态缓存文件存在,直接返回缓存文件 Server.Transfer(fileUrl); } base.OnPreInit(e); } protected string GetFileName() { //我们的缓存文件名由页面文件名加上查询字符串组成 return Request.AppRelativeCurrentExecutionFilePath .ToLower() .Replace(".aspx", "") .Replace("~/", "") .Replace("/", "_") + Request.Url.Query .Replace("?", "_") .Replace("&", "_") + ".html"; } protected override void Render(HtmlTextWriter writer) { StringWriter sw = new StringWriter(); HtmlTextWriter htmlw = new HtmlTextWriter(sw); //调用Render方法,把页面内容输出到StringWriter中 base.Render(htmlw); htmlw.Flush(); htmlw.Close(); //获得页面内容 string pageContent = sw.ToString(); string fullpath = Server.MapPath("~/CacheFile/")+ GetFileName(); string path = Path.GetDirectoryName(fullpath); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } //把页面内容保存到静态文件中 using (StreamWriter stringWriter = File.AppendText(fullpath)) { stringWriter.Write(pageContent); } //将页面内容输出到浏览器 Response.Write(pageContent); } }