OutputCacheAttribute Class

Represents an attribute that is used to mark an action method whose output will be cached.

Namespace:  System.Web.Mvc
Assembly:  System.Web.Mvc (in System.Web.Mvc.dll)

[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Method, Inherited = true, 
	AllowMultiple = false)]
public class OutputCacheAttribute : ActionFilterAttribute, 

The OutputCacheAttribute type exposes the following members.

Public methodOutputCacheAttributeInitializes a new instance of the OutputCacheAttribute class.

Public propertyAllowMultipleGets or sets a value that indicates whether more than one instance of the filter attribute can be specified. (Inherited from FilterAttribute.)
Public propertyCacheProfileGets or sets the cache profile name.
Public propertyStatic memberChildActionCacheGets or sets the child action cache.
Public propertyDurationGets or sets the cache duration, in seconds.
Public propertyLocationGets or sets the location.
Public propertyNoStoreGets or sets a value that indicates whether to store the cache.
Public propertyOrderGets or sets the order in which the action filters are executed. (Inherited from FilterAttribute.)
Public propertySqlDependencyGets or sets the SQL dependency.
Public propertyTypeId (Inherited from Attribute.)
Public propertyVaryByContentEncodingGets or sets the vary-by-content encoding.
Public propertyVaryByCustomGets or sets the vary-by-custom value.
Public propertyVaryByHeaderGets or sets the vary-by-header value.
Public propertyVaryByParamGets or sets the vary-by-param value.

Public methodEquals (Inherited from Attribute.)
Protected methodFinalize (Inherited from Object.)
Public methodGetHashCode (Inherited from Attribute.)
Public methodGetType (Inherited from Object.)
Public methodStatic memberIsChildActionCacheActiveReturns a value that indicates whether a child action cache is active.
Public methodIsDefaultAttribute (Inherited from Attribute.)
Public methodMatch (Inherited from Attribute.)
Protected methodMemberwiseClone (Inherited from Object.)
Public methodOnActionExecutedThis method is an implementation of IActionFilter.OnActionExecuted and supports the ASP.NET MVC infrastructure. It is not intended to be used directly from your code. (Overrides ActionFilterAttribute.OnActionExecuted(ActionExecutedContext).)
Public methodOnActionExecutingThis method is an implementation of IActionFilter.OnActionExecuting and supports the ASP.NET MVC infrastructure. It is not intended to be used directly from your code. (Overrides ActionFilterAttribute.OnActionExecuting(ActionExecutingContext).)
Public methodOnExceptionThis method is an implementation of IExceptionFilter.OnException and supports the ASP.NET MVC infrastructure. It is not intended to be used directly from your code.
Public methodOnResultExecutedThis method is an implementation of IResultFilter.OnResultExecuted and supports the ASP.NET MVC infrastructure. It is not intended to be used directly from your code. (Overrides ActionFilterAttribute.OnResultExecuted(ResultExecutedContext).)
Public methodOnResultExecutingCalled before the action result executes. (Overrides ActionFilterAttribute.OnResultExecuting(ResultExecutingContext).)
Public methodToString (Inherited from Object.)

Output caching lets you store the output of an action method in memory on the Web server. For example, if the action method renders a view, the view page will be cached. This cached page is then available to the application for subsequent requests. Output caching saves your application the time and resources it would take to re-create the result of the action method.

In ASP.NET MVC, you can use the OutputCacheAttribute attribute to mark action methods whose output you want to cache. If you mark a controller with the OutputCacheAttribute attribute, the output of all action methods in the controller will be cached.

The properties contained in OutputCacheAttribute are almost the same as the properties of the @ OutputCache directive. The only @ OutputCache property that is not supported by OutputCacheAttribute is VaryByControl.

Using a Cache Profile

To avoid code duplication, you can set a cache profile in the Web.config file instead of setting cache values individually in pages. You can then reference the profile by using the CacheProfile property of the OutputCache attribute. The following example shows a section of a Web.config file that sets a cache profile. This cache profile will apply to all pages unless the page overrides these settings.

        <add name="MyProfile" duration="60" varyByParam="*" />

The following example shows how to use OutputCacheAttribute to control output caching for the About action method. The attribute sets the cache duration to one minute. Minor changes to the About action method and the About view enable you to see when the view was most recently cached.

If you repeatedly click the tab for the About view, you can see that the page stays cached for 10 seconds, because the OutputCache attribute changed the duration that was set in the Web.config file.

The following example shows the About action method.

The following example shows the About view.

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.