|Important||This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.|
Caching Multiple Versions of a Page
At times you might have a page that you want to cache, but for which different versions are created based on the request. For example, the page might have different output depending on the values passed in the query string.
ASP.NET allows you to cache multiple versions of the same page in the output cache. You can vary the output cache by the following:
The query string in an initial request (HTTP GET).
Control values passed on postback (HTTP POST values).
The HTTP headers passed with a request.
The major version number of the browser making the request.
A custom string in the page. In that case, you create custom code in the Global.asax file to specify the page's caching behavior.
The @ OutputCache directive includes four attributes that enable you to cache multiple versions of page output:
The VaryByParam attribute allows you to vary the cached output depending on the query string.
The VaryByControl attribute allows you to vary the cached output depending on a control value.
The VaryByHeader attribute allows you to vary the cached output depending on the request's HTTP header.
The VaryByCustom attribute allows you to vary the cached output by browser type or by a custom string that you define.
The HttpCachePolicy class provides two properties and a method that allow you to programmatically specify the same cache configuration that you can set declaratively. The VaryByParams and VaryByHeaders properties allow you to specify the query string parameter and header names, respectively, that you want to vary the cache policy by. The SetVaryByCustom method allows you to define custom strings by which to vary the output cache.