asp.net.ph

Skip Navigation LinksASP.NET Applications > ASP.NET Optimization > ASP.NET Caching Features > Caching ASP.NET Pages > Caching Multiple Versions of a Page

Caching Multiple Versions of a Page

ASP.NET Web Applications   ASP .NET Caching Features   Caching ASP.NET Pages


The VaryByCustom Attribute

The VaryByCustom attribute uses either the browser type or other custom strings to output cache multiple versions of a page.

When you include <%@ OutputCache Duration=300 VaryByCustom = "browser" %> at the top of your page, you instruct the output cache to cache varying versions of the page according to the information provided by the page’s Request.Browser.Type property ( browser name and major ID ). If an Internet Explorer browser and a Netscape Navigator 4.0 browser each request a page that you include this directive in, an entry is added for each in the output cache.

You can also use this attribute to extend the functionality of the output cache. To do this, you must include a custom string in the VaryByCustom attribute in the @ OutputCache, and then override the HttpApplication.GetVaryByCustomString method in your application’s Global.asax file. The code that you write in your override statement defines what the output cache varies by.

In the following directive, the custom string is defined as Frames.

<%@ OutputCache Duration=300 VaryByCustom = "Frames" %>

In your application’s Global.asax file, you can include the following code to specify to the output cache whether the requesting browser supports frames, and if it does, instruct it to store a version of the page output in the cache.

public override string GetVaryByCustomString ( HttpContext context, string arg )

   switch ( arg )
      case "Frames":
         return "Frames = " + context.Request.Browser.Frames;
      case "JavaScript":
         return "JavaScript = " + context.Request.Browser.JavaScript;
      default:
         return "";
   }
}
  C# VB

See Also

Caching Portions of an ASP.NET Page   Caching Application Data


Back to top


© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note