asp.net.ph

Skip Navigation LinksHome > ASP.NET Applications > ASP.NET Optimization > ASP.NET Caching Features > Caching ASP.NET Pages

Caching ASP.NET Pages

ASP.NET Web Applications   ASP .NET Caching Features


Setting the Cacheability of a Page

To add a page to the output cache, you must also establish public cacheability for that page.

The cacheability of a Web page defines what Internet devices a document can be cached on. These devices include the client making the request, the Web server responding to the request, and any other cache-capable devices that may be present along the request or response stream, such as proxy servers.

When a Web server sends a response, it passes a Cache-Control HTTP header that, among other things, specifies how documents are to be cached on the network. Depending on the needs of the application, you may choose to define which devices can and cannot cache specific pages.

For example, you may need to use different cacheability settings for a user logon page and for a page that displays a selection of products from a catalog. For security reasons, you should cache the page in the former case only on the server, while the page in the latter scenario can, in all likelihood, be cached on any cache-capable device.

You can set the cacheability of a page’s output declaratively using the @ OutputCache directive, or programmatically using the Response.Cache.SetCacheability method.

The following steps outline the two ways that you can accomplish this.

To set a page’s cacheability declaratively

  1. Include an @ OutputCache directive in the .aspx file and define the required Duration and VaryByParam attributes.
  2. Include a Location attribute in the directive, the possible values of which can be Any, Client, Downstream, Server, or None.
 <%@ OutputCache Duration=60 VaryByParam = "none" Location = "Any"%>

If not specified, the default value for the Location attribute is Any, meaning the page output can be cached on all cache-capable network applications that are involved in the response. These include the requesting client, the origin server, and any proxy servers through which the response passes.

NOTE: The @ OutputCache directive sets the Cache-Control header to Public by default. If you set expirations for a page programmatically, though, you must explicitly set the Cache-Control header to Public using the Response.Cache.SetCacheability method.

To set a page’s cacheability programmatically

  • In the page’s code-declaration block or code-behind class file, include code that sets the cacheability for the page using the Response.Cache.SetCacheability method, the possible values of which can be NoCache, Private, Public, and Server.

The below code shows how to programmatically set the Cache-Control HTTP header to Public.

Response.Cache.SetCacheability ( HttpCacheability.Public );
  C# VB

For technical details regarding HTTP cacheability, see Caching in HTTP and Cache-Control in the World Wide Web Consortium ( W3C ) HTTP 1.1 documentation.

See Also

Caching Multiple Versions of a Page   Caching Portions of an ASP.NET Page   Caching Application Data


Back to top


© 2025 Reynald Nuñez and asp.net.ph. All rights reserved.

If you have any question, comment or suggestion
about this site, please send us a note