How to: Cache Versions of a Page Using Requesting Browser
If your Web application contains a page that creates different output based on the type of the requesting browser, you can cache versions of page's output by the major version of the browser that requests the page. For example, when an Internet Explorer 6 browser requests a page, one version of the page is cached. When a Netscape Navigator browser, another version of Internet Explorer, or any other browser requests the page, another version of the page is added to the output cache.
Note
Major version and browser type information is passed through the MajorVersion property of the HttpBrowserCapabilities object in the current request. For more information, see How to: Detect Browser Types in ASP.NET Web Pages.
To cache multiple versions of a page declaratively based on browser type
In the ASP.NET page, include an @ OutputCache directive with the required Duration and VaryByParam or VaryByControl attributes. The Duration attribute must be set to an integer greater than zero. If you want to cache only by browser type, set the VaryByParam attribute to "None".
In the @ OutputCache directive, include the VaryByCustom attribute and set it to "browser".
The following example will cause the page to be cached for 10 seconds. The output will vary by the browser type.
<%@ OutputCache Duration="10" VaryByParam="None" VaryByCustom="browser" %>
To cache multiple versions of a page programmatically based on browser type.
In the page code, call the SetExpires and SetCacheability methods on the Cache property of the page's Response property.
Call the SetVaryByCustom method, passing the value "browser" in the custom parameter.
The following code example shows how to cache multiple versions of a page for one minute. The output will vary by the type of browser that made the request.
protected void Page_Load(object sender, EventArgs e) { Response.Cache.SetExpires(DateTime.Now.AddMinutes(1d)); Response.Cache.SetCacheability(HttpCacheability.Public); Response.Cache.SetValidUntilExpires(true); Response.Cache.SetVaryByCustom("browser"); }
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load Response.Cache.SetExpires(DateTime.Now.AddMinutes(1.0)) Response.Cache.SetCacheability(HttpCacheability.Public) Response.Cache.SetValidUntilExpires(True) Response.Cache.SetVaryByCustom("browser") End Sub
See Also
Tasks
How to: Set the Cacheability of an ASP.NET Page Declaratively
How to: Set a Page's Cacheability Programmatically
How to: Cache Versions of a Page Using Parameters
How to: Cache Versions of a Page Using HTTP Headers
How to: Cache Versions of a Page Using Custom Strings