Ibahagi sa


Setting the Cacheability of a Page

The cacheability of a page or user control refers to whether a page can be cached on a device during the page's response life cycle. Devices that can cache a page include the browser making the request, the Web server responding to the request, and any other cache-capable devices, such as proxy servers, that are in the request or response stream.

When a Web server sends a response to the requesting browser, the server includes in the response a Cache-Control field in the HTTP header that defines the devices on which the page can be cached. Depending on the needs of your application, you can define which devices should or should not cache individual ASP.NET pages. For example, you might want the cacheability settings for a user logon page to be different from those for a page that displays a selection of products from a catalog. In the case of the logon page, for security reasons you might want to cache the page only on the server, while the catalog page can be cached on any device.

For ASP.NET pages, you can set cacheability by using values in the HttpCacheability enumeration. The enumeration has the following values. The first three map directly to Cache-Control HTTP header settings, and the last three are special values.

  • NoCache   Specifies that the device making the request should get the response from the Web server each time.

  • Public   Allows the response to be cached by clients and shared (proxy) caches.

  • Private   Specifies that the response is cacheable only on the client and not by shared (proxy server) caches.

  • Server   Specifies that the response is cached only at the origin server.

  • ServerAndNoCache   Applies the settings of both Server and NoCache to indicate that the content is cached at the server but all others are explicitly denied the ability to cache the response.

  • ServerAndPrivate   Specifies that a response should be cached only on the origin server and on the requesting client; proxy servers are not allowed to cache the response.

You can set a page's cacheability declaratively by including a Location attribute in the @ OutputCache directive and specifying one of the OutputCacheLocation enumeration values. You can also set a page's cacheability programmatically using the SetCacheability method to specify an HttpCacheability value for the page. The method is accessible through the Cache property of the Response class.

Note

If you use the @ OutputCache directive to set your page's cacheability, you must declare the Duration attribute and either the VaryByControl attribute or the VaryByParam attribute along with the Location attribute. The Duration attribute must be set to a value larger than zero. You can set the VaryByParam attribute to "None" if you do not want to use the functionality of the VaryByParam or VaryByControl parameters. For more information, see How to: Set Expiration Values for ASP.NET Page Caching and Caching Multiple Versions of a Page.

As an alternative to setting a page's cacheability using the @ OutputCache directive, you can create a cache profile in your application's Web.config file and then reference the profile in your page. For more information, see Cache Configuration in ASP.NET.

See Also

Tasks

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

How to: Set a Page's Cacheability Programmatically

Concepts

Caching ASP.NET Pages

Reference

Duration