ASP.NET Syntax ASP.NET Page Syntax Page Directives
Declaratively controls the output caching policies of an ASP.NET page or a user control contained in a page.
<%@ OutputCache Duration = "#ofseconds"
Location = "Any | Client | Downstream | Server | None"
VaryByControl = "controlname"
VaryByCustom = "browser | customstring"
VaryByHeader = "headers"
VaryByParam = "parametername" %>
Duration |
The time, in seconds, that the page or user control is cached. Setting this attribute on a page or user control establishes an expiration policy for HTTP responses from the object and will automatically cache the page or user control output.
NOTE: This attribute is required. If you do not include it, a parser error occurs. |
Location |
One of the OutputCacheLocation enumeration values. The default is Any.
IMPORTANT: This attribute is required when you output cache ASP.NET pages and user controls. A parser error occurs if you fail to include it. |
VaryByControl |
A semicolon-separated list of strings used to vary the output cache. These strings represent fully qualified names of properties on a user control. When this attribute is used for a user control, the user control output is varied to the cache for each specified user control property.
NOTE: This attribute is not supported for @ OutputCache directives in ASP.NET pages. |
VaryByCustom |
Any text that represents custom output caching requirements. If this attribute is given a value of browser, the cache is varied by browser name and major version information. If a custom string is entered, you must override the HttpApplication.GetVaryByCustomString method in your application’s global.asax file. |
VaryByHeader |
A semicolon-separated list of HTTP headers used to vary the output cache. When this attribute is set to multiple headers, the output cache contains a different version of the requested document for each specified header.
NOTE: Setting the VaryByHeader attribute enables caching items in all HTTP/1.1 caches, not just the ASP.NET cache. This attribute is not supported for @ OutputCache directives in user controls. |
VaryByParam |
A semicolon-separated list of strings used to vary the output cache. By default, these strings correspond to a query string value sent with GET method attributes, or a parameter sent using the POST method. When this attribute is set to multiple parameters, the output cache contains a different version of the requested document for each specified parameter. Possible values include none, *, and any valid query string or POST parameter name.
IMPORTANT: This attribute is required when you output cache ASP.NET pages and user controls. A parser error occurs if you fail to include it. If you do not want to specify a parameter to vary cached content, set the value to none. If you want to vary the ouput cache by all parameter values, set the attribute to *. |
Setting values for the page output cache is the same as manipulating the SetExpires and SetCacheability methods of the Response.Cache onject. Setting the VaryByParam attribute when creating a user control implements partial-page caching for that control.
If a Web Forms page requires authorization to be viewed by a user, the output cache sets the Cache-Control
header to private. For more information on all of these subjects, see Caching ASP.NET Pages.
The below code snippet demonstrates how you can set the duration ( in seconds ) that a page or user control is output cached.
<%@ OutputCache Duration = "10" VaryByParam = "none" %>
The next example demonstrates how you can instruct the output cache to cache a page or user control based on parameter values passed by a form’s GET or POST method. Each HTTP request that arrives with a different parameter is cached for ten seconds. Any subsequent requests with the same parameter values are satisfied from the cache until the entry expires.
<%@ OutputCache Duration = "10" VaryByParam = "myParam1; myParam2" %>
Caching ASP.NET Pages Caching Portions of an ASP.NET Page