如何使用 @ OutputCache 指令的 VaryByCustom 属性来缓存不同版本...

来源:百度文库 编辑:神马文学网 时间:2024/04/28 13:46:09

如何使用 @ OutputCache 指令的 VaryByCustom 属性来缓存不同版本的页面

为每个session页配制缓存是通过 @ OutputCache 指令的属性 VaryByCustom 来实现的。按照下面的脚本(包含到一个.aspx文件中),花上10秒钟就可以为各个session ID用户配制独立的页面版本。

<%@ OutputCache Duration="10" VaryByParam="None" VaryByCustom="SessionID" %>

要用到 VaryByCustom="SessionID" ,就要在在服务器的根目录下有Global.asax文件,在文件中声明以下用法:

GetVaryByCustomString 是ASP.NET页面输出缓存的基础。它继承于HttpApplication,返回从session cookie得到的session ID。用户在调用GetVaryByCustomString时会被关联到一个session。

这个技术的缺点是不能用于不支持cookie的用户。另外,用户只有在第二次发出申请时才能获得缓存,因为第一次请求没有合法的session cookie。