Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to stop MVC caching the results of invoking an action method?

I am experiencing a problem with IE caching the results of an action method.

Other articles I found were related to security and the [Authorize] attribute. This problem has nothing to do with security.

This is a very simple "record a vote, grab the average, return the avg and the number of votes" method. The only slightly interesting thing about it is that it is invoked via Ajax and returns a Json object. I believe that it is the Json object that is getting cached.

When I run it from FireFox and watch the XHR traffic with Firebug, everything works perfectly. However, under IE 8 the "throbber" graphic doesn't ever have time to show up and the page elements that display the "new" avg and count that are being injected into the page with jQuery are never different.

I need a way to tell MVC to never cache this action method.

This article seems to address the problem, but I cannot understand it: Prevent Caching of Attributes in ASP.NET MVC, force Attribute Execution every time an Action is Executed

I need a bit more context for the solution to understand how to extend AuthorizationAttribute. Please address your answer as if you were speaking to someone who lacks a deep understanding of MVC even if that means replying with an article on some basics/prerequisites that are required.

Thanks,

Trey Carroll

like image 994
Trey Carroll Avatar asked Jun 03 '10 20:06

Trey Carroll


People also ask

What do you mean by MVC output caching?

The output cache enables you to cache the content returned by a controller action. That way, the same content does not need to be generated each and every time the same controller action is invoked. Imagine, for example, that your ASP.NET MVC application displays a list of database records in a view named Index.

What is the property that used to disable the cache storage in ASP.NET MVC?

You can use the OutputCacheAttribute to control server and/or browser caching for specific actions or all actions in a controller.

What are the different caching techniques available in .NET MVC?

Any (Default)- Content is cached in three locations- the Web Server, any proxy Servers and the Web Browser. Client- Content is cached on the Web Browser. Server- Content is cached on the Web Server. ServerAndClient- Content is cached on the Web Server and the Web Browser.

What is the responsibility of an action method in MVC?

ASP.NET MVC action methods are responsible to execute the request and generate a response to it. All the public methods of the MVC Controller are action methods. If we want the public method to be a non-action method, then we can decorate the action method by “NonAction” attribute.


1 Answers

MVC doesn't cache the results. IE does.

So you have to tell IE not to do it.

Here's how I do it. First, an attribute:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
public sealed class CacheControlAttribute : ActionFilterAttribute
{
    public CacheControlAttribute(HttpCacheability cacheability)
    {
        this._cacheability = cacheability;
    }

    public HttpCacheability Cacheability { get { return this._cacheability; } } 

    private HttpCacheability _cacheability;

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        HttpCachePolicyBase cache = filterContext.HttpContext.Response.Cache;
        cache.SetCacheability(_cacheability);
    }
}

Next, an action:

    [CacheControl(HttpCacheability.NoCache), HttpGet]
    public JsonResult MyAction()
like image 134
Craig Stuntz Avatar answered Nov 17 '22 07:11

Craig Stuntz