|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. ArchiveDisclaimer|
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 theenumeration. The enumeration has the following values. The first three map directly to Cache-Control HTTP header settings, and the last three are special values.
Specifies that the device making the request should get the response from the Web server each time.
Allows the response to be cached by clients and shared (proxy) caches.
Specifies that the response is cacheable only on the client and not by shared (proxy server) caches.
Specifies that the response is cached only at the origin server.
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.
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 aattribute in the directive and specifying one of the enumeration values. You can also set a page's cacheability programmatically using the method to specify an HttpCacheability value for the page. The method is accessible through the property of the class.
If you use the @ OutputCache directive to set your page's cacheability, you must declare theattribute and either the attribute or the 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 and .
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.