|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.|
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.
To set a page's cacheability declaratively
Include andirective in the page and define and attributes.
Include aattribute in the @ OutputCache directive and define its value as one of the following values in the enumeration: , , , , , or .
The following code shows how to set the page's cacheability to 60 seconds:
<%@ OutputCache Duration="60" VaryByParam="None"%>
The default setting is Any. If you do not define a Location attribute, the page output can be cached on all cache-capable network devices that are involved in the response. These include the requesting client, the origin server, and any proxy servers through which the response passes.
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 andirective in each ASP.NET page that uses the profile, and set the 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
<%@ OutputCache CacheProfile="Cache30Seconds" %>