Click here to Skip to main content
15,394,707 members
Articles / Web Development / ASP.NET
Posted 16 Feb 2009


21 bookmarked

ASP.NET MVC Result Cache

Rate me:
Please Sign up or sign in to vote.
3.67/5 (9 votes)
17 Feb 2009GPL3
Cache the ActionResult using an ActionFilter


The idea behind this cache approach is plain simple: the web application should cache ActionResults that demand heavy CPU/DB time to load. Currently, the ASP.NET MVC Framework has a cache feature, the OutputCache, that works by storing a copy of the resulting webpage. This feature does not fit a scenario where the webpage is dependant, for example, on session data.

Using the Code

Add the ResultCache attribute to an action of a controller.

public ActionResult About()
    //code that demands heavy CPU/DB time to execute 

    ViewData["DummyData"] = dummy;

    return View();

Here is the ViewPage code:

<%@ Page Language="C#" Inherits="System.Web.Mvc.ViewPage" %>
Sample ViewData value: <%=ViewData["DummyData"]%>
Sample Session dependant data <%=Session["UserName"] %>

The code in the ViewPage is not affected by using the ResultCache. In the sample, the View shows the data from the ViewData (can be cached) and the Session.

How It Works

After the Action executes, the Action Filter stores the ActionResult in the application Cache.

public override void OnActionExecuted(ActionExecutedContext filterContext)
	//Add the ActionResult to cache	
    filterContext.HttpContext.Cache.Add(this.CacheKey, filterContext.Result, 
      Dependency, DateTime.Now.AddSeconds(Duration), 
      System.Web.Caching.Cache.NoSlidingExpiration, Priority, null);

	//Add a value in order to know the last time it was cached.
    filterContext.Controller.ViewData["CachedStamp"] = DateTime.Now;


The next time the Action is invoked, the attribute will retrieve the result from cache preventing the Action from executing.

public override void OnActionExecuting(ActionExecutingContext filterContext)
    string url = filterContext.HttpContext.Request.Url.PathAndQuery;
    this.CacheKey = "ResultCache-" + url;

    if (filterContext.HttpContext.Cache[this.CacheKey] != null)
        //Setting the result prevents the action itself to be executed
        filterContext.Result = 


About the Code

This cache approach is used in the open source MVC site
Get the full source code of the site at CodePlex.


  • February 16, 2009 - Article submitted
  • February 17, 2009 - Code comments improved


This article, along with any associated source code and files, is licensed under The GNU General Public License (GPLv3)


About the Author

Jorge Bay Gondra
Software Developer
Spain Spain
Jorge has been working in Software development for more than 10 years. Born in Argentina, he lives in Spain since 2004.

He worked as a consultant for mayor companies including Log, HP and Avanade and holds some technical certifications including MCSD and MCAD.

He is the founder of the mvc forum open source project Nearforums, the Node.js Cassandra driver and the owner of the news release site

Follow him on Twitter:

Contact: jorgebaygondra at gmail

Comments and Discussions

GeneralGood example Pin
net_201018-Apr-13 1:48
Membernet_201018-Apr-13 1:48 
GeneralAwesome solution! Pin
yookunkun27-Apr-11 21:18
Memberyookunkun27-Apr-11 21:18 
GeneralMy vote of 5 Pin
Jaguire24-Sep-10 6:02
MemberJaguire24-Sep-10 6:02 
GeneralGood to know Pin
Donsw5-Mar-09 8:32
MemberDonsw5-Mar-09 8:32 
GeneralMy vote of 1 Pin
AlKatawazi18-Feb-09 9:56
MemberAlKatawazi18-Feb-09 9:56 
GeneralRe: My vote of 1 Pin
Jorge Bay Gondra18-Feb-09 22:17
MemberJorge Bay Gondra18-Feb-09 22:17 
GeneralGreat article... Pin
Jose Maria Estrade16-Feb-09 5:26
MemberJose Maria Estrade16-Feb-09 5:26 
RantAlready been done by the Framework! Pin
AlKatawazi18-Feb-09 9:55
MemberAlKatawazi18-Feb-09 9:55 
GeneralRe: Already been done by the Framework! Pin
Jorge Bay Gondra18-Feb-09 22:16
MemberJorge Bay Gondra18-Feb-09 22:16 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.