Menus_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 authenticated
7 return "B";
8 }
9 else
10 {
11 // Request is not authenticated
12 return "C";
13 }
14 }
根據此思路我們可以開發一個依浏覽器類型不同的緩存頁面的例子
例如我們現有頁面WebForm3.aspx,我們可以根據訪問著的浏覽器類型來做頁面緩存
首先在頁面中加入
<%@ OutputCache Duration="600" VaryByParam="none" VaryByCustom="ietype" %>