Export (0) Print
Expand All

Caching Versions of a Page, Based on Requesting Browser

You can use the @ OutputCache directive's VaryByCustom attribute or the HttpCachePolicy.SetVaryByCustom method to vary the versions of page output by the major version of the browser that requests the page. This major version and browser type information is passed through the HttpBrowserCapabilities.MajorVersion property associated with the current request. You could access this information through the Request object associated with the page, but it is simpler to use the built-in functionality offered by ASP.NET.

To cache multiple versions of a page output declaratively based on browser type

  1. 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, you must set its value to None.
  2. In the body of the @ OutputCache directive, include the VaryByCustom attribute and set it to browser.
    <%@ OutputCache Duration="10" VaryByParam="None" VaryByCustom="browser" %>
    

To cache multiple versions of a page response programmatically, based on custom strings

  1. 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.
  2. In the same code, specify browser in the custom parameter of the SetVaryByCustom method.
    Response.Cache.SetVaryByCustom("browser");
    [Visual Basic]
    Response.Cache.SetVaryByCustom("browser")
    

Regardless of which of these techniques you use, when you specify browser, ASP.NET automatically caches versions of the page output based on the browser name and major version information passed in the Request.Browser.Type property, with the current request. This information is available through the HttpContext class associated with the request.

For example, when an Internet Explorer 5 browser requests a page, the version of the page is cached for the string value "IE5" that is passed in the Type property for the request. 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.

See Also

@ OutputCache | Caching ASP.NET Pages | HttpCachePolicy.SetVaryByCustom | HttpResponse Class | HttpCachePolicy Class

Show:
© 2014 Microsoft