다음을 통해 공유


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.

You can cache multiple versions of a page output declaratively using attributes of the @ OutputCache directive, and programmatically using the properties and methods of the HttpCachePolicy class.

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.

    Note

    You must include either the VaryByParam attribute or the VaryByControl attribute in any @ OutputCache directive. However, if you do not need to vary your cached output by control or parameters, you can define VaryByParam with its value to None.

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.

See Also

Tasks

How to: Set the Cacheability of an ASP.NET Page Declaratively

How to: Set a Page's Cacheability Programmatically

How to: Cache Versions of a Page Using Requesting Browser

How to: Cache Versions of a Page Using Parameters

How to: Cache Versions of a Page Using HTTP Headers

How to: Cache Versions of a Page Using Custom Strings

Concepts

Caching ASP.NET Pages

Setting the Cacheability of a Page