How to: Set the Cacheability of an ASP.NET Page Declaratively
The cacheability of a page or user control refers to whether or not a page can be cached on a device during its response life cycle. These devices include the client (browser) making the request, the Web server responding to the request, and any cache-capable devices, such as proxy servers, that are in the request or response stream.
If you know at design time what cacheability setting you need for a page, you can set cacheability declaratively. The page will then use the same cacheability settings for all requests. For more information, see Setting the Cacheability of a Page.
To set a page's cacheability declaratively
Include an @ OutputCache directive in the page and define Duration and VaryByParam attributes.
Include a Location attribute in the @ OutputCache directive and define its value as one of the following values in the OutputCacheLocation enumeration: Any, Client, Downstream, Server, ServerAndClient, or None.
The following code shows how to set the page's cacheability to 60 seconds:
<%@ OutputCache Duration="60" VaryByParam="None"%>
To set a page's cacheability declaratively using a cache profile
Define a cache profile in your application's Web.config file and in the profile, include duration and varyByParam settings.
The following <caching> configuration element defines a cache profile named Cache30Seconds, which will cache the page on the server for 30 seconds.
<caching> <outputCacheSettings> <outputCacheProfiles> <add name="Cache30Seconds" duration="30" varyByParam="none" /> </outputCacheProfiles> </outputCacheSettings> </caching>
Include an @ OutputCachedirective in each ASP.NET page that uses the profile, and set the CacheProfile attribute to the name of the cache profile defined in your Web.config file.
The following code specifies that the page should use the cache profile named Cache30Seconds:
<%@ OutputCache CacheProfile="Cache30Seconds" %>
See Also
Tasks
How to: Set a Page's Cacheability Programmatically