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.

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.