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


Often, an application may have a Web page that can receive different requests for different information that will be rendered on the same page; hence, the presentation of the information will mostly be changed for each request.

For example, if a page is meant to allow users to provide different sets of inputs to a database query, the page response can be different for each request. So, depending on the purpose, a single ASP.NET page can generate a number of possible responses.

This requires a slightly different approach from that of output caching pages that do not vary in content. Fortunately in ASP.NET, you can simply choose to cache any or all of the possible versions of a page’s output. This document takes a closer look at the different methods you can use to accomplish that.

Basically, ASP.NET allows caching multiple versions of a page response in either of two ways:

  • at design time, by declaratively using variable attributes on the @ OutputCache directive, or
  • at run time, by programmatically using the corresponding properties and methods of the Response.Cache class.

Specifically, the @ OutputCache directive includes three attributes that allow you to cache different versions of page output:

  • the required VaryByParam attribute enables varying the cached output depending on the GET or POST parameters passed with the request.
  • the VaryByHeader attribute enables varying the cached output depending on the HTTP header associated with the request.
  • the VaryByCustom attribute enables varying the output cache by browser type or by a custom string that you define.

NOTE: While you must include the VaryByParam attribute in every @ OutputCache directive, you can set its value to None if you do not need to use the functionality it provides.

Alternatively, the Response.Cache object provides two properties and a method that allow you to do the same things that the @ OutputCache attributes do, programmatically.

  • the Response.Cache.VaryByParams property
  • the Response.Cache.VaryByHeaders property
  • the Response.Cache.SetVaryByCustom method

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