Output Caching in an Action Method

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.

OutputCache Attribute Properties

The following table shows the properties of the OutputCacheAttribute attribute.

Property

Description

Duration

The time, in seconds, that the output is cached.

Location

One of the OutputCacheLocation enumeration values. The default is Any.

Shared

A Boolean value that determines whether the output can be shared with multiple pages. The default is false.

VaryByCustom

Any text that represents custom output-caching requirements.

VaryByHeader

A semicolon-separated list of HTTP headers that are used to vary the output cache.

VaryByParam

A semicolon-separated list of strings that correspond to query-string values for the GET method, or to parameter values for the POST method.

VaryByContentEncodings

A semicolon-separated list of strings that are used to vary the output cache. The VaryByContentEncodings attribute is used with the Accept-Encoding header to determine how cached responses are served for different content encodings.

CacheProfile

The name of the cache settings to associate with the page.

NoStore

A Boolean value that determines whether to prevent secondary storage of the cached information.

SqlDependency

A string value that identifies a set of database-name and table-name pairs that the action's output cache depends on.

The properties listed in the table 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 it 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 by using the cache profile set. 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>

Example Application

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>

See Also

Other Resources

Action Filtering in MVC Applications