16.3. Caching Pages Based on Browser Type and Version

Problem

You have a page with static content that you want to cache, but the page is rendered differently for some browsers.

Solution

Add the @ OutputCache directive to the .aspx file of each page you want to cache with the VaryByCustom attribute set to "browser“:

	<%@ Page Language="VB" MasterPageFile="~/ASPNetCookbookVB.master"
         AutoEventWireup="false"
		 CodeFile="CH16CacheByCustomStringVB.aspx.vb"
		 Inherits="ASPNetCookbook.VBExamples.CH16CacheByCustomStringVB"
		 Title="Cache By Custom String" %>
    <%@ OutputCache Duration="10" VaryByParam="none"
                       VaryByCustom="browser" %>
           …

Discussion

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

Get ASP.NET 2.0 Cookbook, 2nd Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.