Welcome 微信登录

首页 / 网页编程 / ASP.NET / 代码阅读总结之Fitch and Mather 7.0(自定义字符串缓存页)

代码阅读总结之Fitch and Mather 7.0(自定义字符串缓存页)2012-01-18 博客园 aierongMenus_ascx中我们看到用了缓存自定义字符串"authenticated"

<%@ OutputCache Duration="86400" VaryByParam="None" VaryByCustom="authenticated" %>

注意: @OutputCache 指令与必需的 Duration 和 VaryByParam 属性包括在一起。必须将 Duration 属性设置为大于零的任意整数。如果不想使用 VaryByParam 属性提供的功能,请将其值设置为 None

在Global.asax文件中重写GetVaryByCustomString方法

此处是根据用户是否验证来缓存用户控件,即一个通过验证的用户控件,一个未验证的用户控件

1public override string GetVaryByCustomString(HttpContext context, string custom)2    {3    // There are two different possible caching cases here so we return a different string in each one.4    if(context.Request.IsAuthenticated)5      {6      // Request is authenticated7      return "B";8      }                9    else10      {11      // Request is not authenticated12      return "C";13      }      14    }
根据此思路我们可以开发一个依浏览器类型不同的缓存页面的例子

例如我们现有页面WebForm3.aspx,我们可以根据访问着的浏览器类型来做页面缓存

首先在页面中加入

<%@ OutputCache Duration="600" VaryByParam="none" VaryByCustom="ietype" %>