You have a page with static content that you want to cache, but the page is rendered differently for some browsers.
Add the @ OutputCache
directive to the
.aspx
file of each page you want to cache with
the VaryByCustom
attribute set to
"browser
“, as shown here:
<%@ Page Language="vb" AutoEventWireup="false" Codebehind="CH13CacheByBrowserVB.aspx.vb" Inherits="ASPNetCookbook.VBExamples.CH13CacheByBrowserVB" %><%@ OutputCache Duration="30" VaryByParam="none"
VaryByCustom="browser" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> <html> ... </html>
The way in which a page is rendered often depends on the characteristics and version number of the browser making the request. ASP.NET handles most of this variation behind the scenes by sensing the browser type and its version and then rendering the HTML and JavaScript in a suitable format.
The variation in a rendered page from one browser type to another can cause problems if you implement caching as described in Recipe 13.1. For example, if a request is made for a page of this type by Internet Explorer 6.x, the page is rendered and stored in the cache. If a request for the same page is made by a Netscape 4.x browser before the cached copy expires, the Internet Explorer 6.x version will be sent to the browser. This generally results in a poorly displayed or improperly functioning page.
ASP.NET provides the ability to cache browser-specific versions of
the rendered page by setting the VaryByCustom
attribute of the OutputCache
element to
"browser
“. When VaryByCustom
is
set to "browser
“, ASP.NET will check the browser
name and major version of the browser making the page request (4.x,
5.x, 6.x, and the like) to see if there is a copy of the rendered
page stored in the cache that matches them. If so, the cached version
is sent as the response. Otherwise, the page will be rendered as
usual, stored in the cache, and then delivered to the browser.
The only VaryByCustom
attribute value that ASP.NET
provides built-in support for is "browser
“;
however, with additional coding, this attribute can be used to
control the caching of a page by any variation you choose.
Recipe 13.4
provides an example of using the VaryByCustom
attribute to cache a page based on the browser type and full version
number.
Recipe 13.4 for an example of caching based on browser type and full version number