OutputCacheAttribute Class

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

Inheritance Hierarchy

System.Object
  System.Attribute
    System.Web.Mvc.FilterAttribute
      System.Web.Mvc.ActionFilterAttribute
        System.Web.Mvc.OutputCacheAttribute

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

Syntax

'Declaration
<AttributeUsageAttribute(AttributeTargets.Class Or AttributeTargets.Method, Inherited := True,  _
    AllowMultiple := False)> _
Public Class OutputCacheAttribute _
    Inherits ActionFilterAttribute
[AttributeUsageAttribute(AttributeTargets.Class|AttributeTargets.Method, Inherited = true, 
    AllowMultiple = false)]
public class OutputCacheAttribute : ActionFilterAttribute
[AttributeUsageAttribute(AttributeTargets::Class|AttributeTargets::Method, Inherited = true, 
    AllowMultiple = false)]
public ref class OutputCacheAttribute : public ActionFilterAttribute

The OutputCacheAttribute type exposes the following members.

Constructors

  Name Description
Public method OutputCacheAttribute Initializes a new instance of the OutputCacheAttribute class.

Top

Properties

  Name Description
Public property CacheProfile Gets or sets the cache profile name.
Public property Duration Gets or sets the cache duration, in seconds.
Public property Location Gets or sets the location.
Public property NoStore Gets or sets a value that indicates whether to store the cache.
Public property Order Gets or sets the order in which the action filters are executed. (Inherited from FilterAttribute.)
Public property SqlDependency Gets or sets the SQL dependency.
Public property TypeId When implemented in a derived class, gets a unique identifier for this Attribute. (Inherited from Attribute.)
Public property VaryByContentEncoding Gets or sets the vary-by-content encoding.
Public property VaryByCustom Gets or sets the vary-by-custom value.
Public property VaryByHeader Gets or sets the vary-by-header value.
Public property VaryByParam Gets or sets the vary-by-parameter value.

Top

Methods

  Name Description
Public method Equals Infrastructure. Returns a value that indicates whether this instance is equal to a specified object. (Inherited from Attribute.)
Protected method Finalize Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection. (Inherited from Object.)
Public method GetHashCode Returns the hash code for this instance. (Inherited from Attribute.)
Public method GetType Gets the Type of the current instance. (Inherited from Object.)
Public method IsDefaultAttribute When overridden in a derived class, indicates whether the value of this instance is the default value for the derived class. (Inherited from Attribute.)
Public method Match When overridden in a derived class, returns a value that indicates whether this instance equals a specified object. (Inherited from Attribute.)
Protected method MemberwiseClone Creates a shallow copy of the current Object. (Inherited from Object.)
Public method OnActionExecuted Called by the MVC framework after the action method executes. (Inherited from ActionFilterAttribute.)
Public method OnActionExecuting Called by the MVC framework before the action method executes. (Inherited from ActionFilterAttribute.)
Public method OnResultExecuted Called by the MVC framework after the action result executes. (Inherited from ActionFilterAttribute.)
Public method OnResultExecuting Called before the action result executes. (Overrides ActionFilterAttribute.OnResultExecuting(ResultExecutingContext).)
Public method ToString Returns a string that represents the current object. (Inherited from Object.)

Top

Explicit Interface Implementations

  Name Description
Explicit interface implemetationPrivate method _Attribute.GetIDsOfNames Maps a set of names to a corresponding set of dispatch identifiers. (Inherited from Attribute.)
Explicit interface implemetationPrivate method _Attribute.GetTypeInfo Retrieves the type information for an object, which can be used to get the type information for an interface. (Inherited from Attribute.)
Explicit interface implemetationPrivate method _Attribute.GetTypeInfoCount Retrieves the number of type information interfaces that an object provides (either 0 or 1). (Inherited from Attribute.)
Explicit interface implemetationPrivate method _Attribute.Invoke Provides access to properties and methods exposed by an object. (Inherited from Attribute.)

Top

Remarks

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.

<system.web>
  <caching>
    <outputCacheSettings>
      <outputCacheProfiles>
        <add name="MyProfile" duration="60" varyByParam="*" />
      </outputCacheProfiles>
    </outputCacheSettings>
  </caching>
</system.web>

Examples

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.

<OutputCache(CacheProfile:="MyProfile", Duration:=10)> _
Function About() As ActionResult
    ViewData("Message") = "This page was cached at " & DateTime.Now

    Return View()
End Function
[OutputCache(CacheProfile = "MyProfile", Duration = 10)]
public ActionResult About()
{
    ViewData["Message"] = "This page was cached at " + DateTime.Now;

    return View();
}

The following example shows the About view.

<asp:Content ID="aboutTitle" ContentPlaceHolderID="TitleContent" runat="server">
    About Us
</asp:Content>

<asp:Content ID="aboutContent" ContentPlaceHolderID="MainContent" runat="server">
    <h2>About</h2>
    <p>
        <%= Html.Encode(ViewData("Message")) %>
    </p>
</asp:Content>
<asp:Content ID="aboutTitle" ContentPlaceHolderID="TitleContent" runat="server">
    About Us
</asp:Content>

<asp:Content ID="aboutContent" ContentPlaceHolderID="MainContent" runat="server">
    <h2>About</h2>
    <p>
        <%= Html.Encode(ViewData["Message"]) %>
    </p>
</asp:Content>

Thread Safety

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

See Also

Reference

System.Web.Mvc Namespace

Other Resources

Action Filtering in ASP.NET MVC Applications

Extending Metadata Using Attributes