Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

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.


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

  1. 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".

  2. 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.

  1. In the page code, call the SetExpires and SetCacheability methods on the Cache property of the page's Response property.

  2. 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)

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    End Sub

Community Additions

© 2015 Microsoft