ASP.NET页面如何建立静态缓存
2011-02-11 17:17
561 查看
方法A:使用 HttpModule 技术拦截页面访问,导向静态缓存页
步骤:
1、创建一个新的HtppModule,拦截对aspx类型页面的访问,判断是否有静态缓存页
此 HttpMudole 类文件放入 App_Code 目录
2、创建一个Page子类,重写Render方法,在其中将页面生成的最终结果保存在指定目录下
需要建立缓存的页面,继承此类即可。
方法B、不拦截所有的aspx页面,由具有静态缓存功能的网页自己判断是否该加载静态页
需要建立静态缓存的页面,将默认的继承 Page 修改为继承此 StaticFileCachePageBase 类即可
(可以用在只有少数页面需要缓冲的情况下)
步骤:
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(); 4000 string path = Path.GetDirectoryName(fullpath); if (!Directory.Exists(path)) { Directory.CreateDirectory(path); } //把页面内容保存到静态文件中 using (StreamWriter stringWriter = File.AppendText(fullpath)) { stringWriter.Write(pageContent); } //将页面内容输出到浏览器 Response.Write(pageContent); } }
相关文章推荐
- ASP.NET页面如何建立静态缓存
- asp.net里如何清空页面缓存的后台代码
- ASP.NET Core应用针对静态文件请求的处理[5]: DefaultFilesMiddleware中间件如何显示默认页面
- ASP.Net MVC如何访问的静态页面
- ASP.NET 优化管道 缓存设置 静态页面设置缓存
- ASP.NET中如何对页面输出缓存
- Asp.net如何生成html静态页面
- ASP.Net MVC如何访问的静态页面
- asp.net如何清除页面缓存?
- 在ASP.NET动态页中生成静态页面
- Asp.Net中动态页面转静态页面
- 页面中包含同一个asp.net控件多次,如何产生不同的javascript方法名
- ASP.net中的页面数据缓存技术
- ASP.Net 更新页面输出缓存的几种方法(包括用户控件,iframe,页面缓存等)
- 如何在ASP.NET环境中建立基于WML的站点
- asp.net传递的中文url参数在asp页面如何正确接收
- 如何在ASP.NET页面中使用异步任务(PageAsyncTask)
- ASP.Net中如何关闭当前页面
- Asp.net禁用页面缓存的方法
- 如何给你的ASP.NET页面添加HelpPage