Home > ASP.NET Applications > ASP.NET Optimization > ASP.NET Caching Features > Setting the Output Cache Location
ASP.NET Web Applications ASP .NET Caching Features
You can set the cacheability of page or user control output by using the Location attribute of the @ OutputCache directive, or by specifying HttpCacheability enumeration values in the page’s Response.Cache property ( HttpResponse.Cache ). Either of these techniques set the Cache-Control HTTP header for a response.
NOTE: If you use the @ OutputCache directive and set the Location attribute to any supported value other than None, the output cache’s location information will not be valid unless you use the Duration attribute to set expirations for the page response. However, if you use the HttpCachePolicy class to set cacheability, you do not need to set expirations if you have established a validation policy.
The default value for the Location attribute is Any, which allows the page output to be cached on all cache-capable network applications that are involved in the response. This includes the requesting client, the responding server, or a proxy server through which the response passes. This default behavior is equivalent to the following code.
Response.Cache.SetCacheability ( HttpCacheability.Public );
Response.Cache.SetCacheability ( HttpCacheability.Public ) |
|
C# |
VB |
You may need to change these default settings, depending on the needs of your page or application. For example, if a page requires authentication and you want to cache its output, change the Cache-Control header to private. The following @ OutputCache directive declared at the top of the page does this, and also sets the amount of time that the page is cached to 60 seconds.
<%@ OutputCache Duration=60 Location = "Client" %>
You would have to apply the following code in your page or application to achieve the same result.
Response.Cache.SetExpires ( DateTime.Now.AddSeconds ( 60 ) );
Response.Cache.SetCacheability ( HttpCacheability.Private );
The @ OutputCache directive is much simpler than the code that it automatically calls when you use it.
To enable caching only on cache-capable applications in the request stream between the client and the Web server, such as a proxy server, set the Location attribute to Downstream. This sets the cacheability of the page output to Public, but uses the SetNoServerCaching method to disallow caching the page output on the Web server.
For example, including <%@ OutputCache Duration=60 Location = "Downstream" %>
at the top of your page is the equivalent of the following code.
Response.Cache.SetExpires ( DateTime.Now.AddSeconds ( 60 ) );
Response.Cache.SetCacheability ( HttpCacheability.Public );
Response.Cache.SetNoServerCaching ( );
Response.Cache.SetExpires ( DateTime.Now.AddSeconds ( 60 ) )
Response.Cache.SetCacheability ( HttpCacheability.Public )
Response.Cache.SetNoServerCaching ( ) |
|
C# |
VB |
You can also specify that a page’s output is cached only on the server by setting the Location attribute to Server. This disables client and downstream caching, ensures that the origin server controls the cacheability of the output, and also ensures that all clients must request the document from the server. All requests for the document are served from the server cache for the specified duration.
You can also disable output caching for the page entirely by setting the Location attribute to None. This is the equivalent of using the HttpCacheability.NoCache field.
NOTE: Using the Location attribute in the @ OutputCache directive on a page has no effect on user controls that are enabled for output caching. For example, if you set Location=None at the page level, a user control in that page that contains its own @ OutputCache directive is still cached.
Caching ASP.NET Pages @ OutputCache Directive