views:

446

answers:

1

Since upgrading from mvc 2 beta 2 to rc I'm having trouble with an ajax submission in Internet Explorer. Upon carrying out a jquery form post, the function returns a url to a controller action. This worked fine with the controller action picking up it was a ajaxrequest and then piping back a partial view to update the page. This still works fine in Firefox, however in Internet Explorer the final call to the controller action now comes from cache and returns therefore returns a full view rather than partial.

I've tried setting the outputcache to 0 with no success and I've also tried the nocache actionfilter as described here http://stackoverflow.com/questions/1160105/asp-net-mvc-disable-browser-cache with no luck. The only way I can stop IE from pulling from cache is to physically delete the cached version.

Anyone have any ideas (apologies if this isn't very clear, tricky one to explain!)?

+1  A: 

For some reason, IE is really aggressive about caching AJAX GETs. So if you are fetching that via AJAX, the behavior is not surprising to me. Also not surprising is that using output cache attribute didn't fix the problem, because it is IE, rather than the server, which is doing the caching. What you need to do is to tell IE not to cache the request, by setting the appropriate headers in the HTTP. Here is how we do it:

    [CacheControl(HttpCacheability.NoCache), HttpGet]
    public JsonResult DoStuff()
    {
        //...
    }

public class CacheControlAttribute : ActionFilterAttribute
{
    public CacheControlAttribute(HttpCacheability cacheability)
    {
        this._cacheability = cacheability;
    }

    private HttpCacheability _cacheability;

    public override void OnActionExecuted(ActionExecutedContext filterContext)
    {
        HttpCachePolicyBase cache = filterContext.HttpContext.Response.Cache;
        cache.SetCacheability(_cacheability);
    }
}
Craig Stuntz
Thanks for this, strange thing is this system was working fine prior to upgrading to the release candidate. I've implemented your custom attribute but I'm still having the same problem unfortunately.
Steve C
Look closely at (1) the headers IE is sending and (2) the headers in your response. You can use Fiddler for this. If you see IE making no request, it is certainly IE, not the server, doing the caching, and you need to craft the headers in the *initial* page load (the one the server actually did respond to) such that IE won't cache it.
Craig Stuntz
I've added the nocache filter to the entire controller and it definitely catches it on initial load (checked via breakpoint). The request, as you say is not being made and is coming from cache (according to debugbar). I've opened my dev box up at http://cancharda.selfip.com:51331/Admin/Events as an example, if you click on the pencil icon next to either event, a dialog opens, by clicking the save the issue becomes apparent. Thanks for your help, I think I'm going mad! :)
Steve C
Try setting the `Expires` header in the past. ISTM old versions of IE didn't respect `Cache-Control`. See also http://www.mnot.net/cache_docs/
Craig Stuntz
Fantastic, that has got the listing side of things working, strangely applying the same fix to the initial action isn't doing the same? But original problem fixed, thanks very much!!!
Steve C