Caching Versions of a Page, Based on Custom Strings
In addition to varying the output cache by browser type, the @ OutputCache directive's VaryByCustom attribute and the HttpCachePolicy.SetVaryByCustom method allow you to cache multiple versions of page output by a custom string that you define.
When you choose to extend the functionality of the output cache, using either of these techniques, you must override the HttpApplication.GetVaryByCustomString method in your application's Global.asax file. The code that you write in your override statement defines how your application caches any page output that is varied by the custom string or strings that you specify.
To cache multiple versions of page output declaratively, based on custom strings
In the .aspx file, include an @ OutputCache directive with the required Duration and VaryByParam attributes. The Duration attribute must be set to any integer greater than zero. If you do not want to use the functionality provided by the VaryByParam attribute, set its value to None.
In the body of the @ OutputCache directive, include the VaryByCustom attribute that is set to the string that you want to vary the output cache by. The following directive varies the page output by the custom string "
minorversion
".<%@ OutputCache Duration="10" VaryByParam="None" VaryByCustom="minorversion" %>
In a code-declaration block in your application's global.asax file, override the GetVaryByCustomString method to specify the behavior of the output cache for the custom string.
To cache multiple versions of page output programmatically, based on custom strings
In the page's code-declaration block or code-behind class, use Response.Cache syntax to set the expiration and visibility policies for the cached page content. You accomplish this by using the HttpCachePolicy.SetExpires and HttpCachePolicy.SetCacheability methods, respectively. For more information, see Setting Expirations for Page Caching and Setting the Cacheability of a Page.
In the same code, specify the custom string in the custom parameter of the SetVaryByCustom method. The following code sets the custom string to "
minorversion
".Response.Cache.SetVaryByCustom("minorversion"); [Visual Basic]Response.Cache.SetVaryByCustom("minorversion")
In a code-declaration block in your application's Global.asax file, override the GetVaryByCustomString method to specify the behavior of the output cache for the custom string.
The following example demonstrates a simple Global.asax file that overrides the GetVaryByCustomString method when a page in the application passes the custom string minorversion
to it.
<script>
public override string GetVaryByCustomString(HttpContext context, string arg){
If (arg = "minorversion"){
Return "Version=" + context.Request.Browser.MinorVersion.ToString();
}
}
</script>
[Visual Basic]<script>
Public Overrides Function GetVaryByCustomString(context As HttpContext, arg As String) As String
If (arg = "minorversion") Then
return "Version=" & context.Request.Browser.MinorVersion.ToString()
End If
End Function
</script>
The following example is a page that interacts with the previous Global.asax file to vary the output cache by the minorversion
custom string.
Note This sample page uses declarative syntax to vary the output cache. If you want to do this programmatically, place code similar to that shown in Setting Expirations for Page Caching between the opening and closing tags of the
<
script>
element.
<%@ OutputCache Duration="30" VaryByParam="none" VaryByCustom="minorversion" %>
<Script language="C#" runat="server">
public void Page_Load(Object sender, EventArgs e) {
browserversion.InnerHtml = Request.Browser.MinorVersion.ToString();
timestamp.InnerHtml = DateTime.Now.ToString("r");
}
</Script>
Varying output by custom string (querystring value): <B id="browserversion" runat="server"/>
<BR>
Added to the cache: <B id="timestamp" runat="server" />
[Visual Basic]<%@ Page Language="VB" %>
<%@ OutputCache Duration="30" VaryByParam="none" VaryByCustom="minorversion" %>
<Script runat="server">
Public Sub Page_Load
browserversion.InnerHtml = Request.Browser.MinorVersion.ToString()
timestamp.InnerHtml = DateTime.Now.ToString("r")
End Sub
</Script>
Varying output by browser: <B id="browserversion" runat="server"/>
<BR>
Added to the cache: <B id="timestamp" runat="server" />
When the page in this example is requested with a different value passed to the HttpBrowserCapabilities.MinorVersion property value, a different version of the page output is cached for each browser that makes a request. For example, a request from an Internet Explorer 5 browser causes one version to be cached, while a request from an Internet Explorer 5.5 browser causes another to be cached.
See Also
@ OutputCache | Caching ASP.NET Pages | HttpApplication.GetVaryByCustomString Method | VaryByParams | VaryByHeaders