AaBa
AaBa

Reputation: 471

IE11 Caching issue using MVC 5

I got a browser page caching issue specifically with IE 11, the SomePage page is always loaded from browser cache. The same page work's perfectly in Edge, Chrome and FF.

The page is called from a JS function as below:

$.ajaxJsonAntiForgery({
        type: "POST",
        url: '@Html.Raw(Url.Action("SomeAPI", "cntrl"))',
        dataType: "json",
        data: { model: model },
        success: function (result) {
            if (result.Success) {
                window.location = '@Html.Raw(Url.Action("SomePage", "cntrl"))';
        ...

I have tried following approached:

  1. Appending some random number with the URL. - Didn't work

  2. Added following code in action method : - Didn't work

    Response.Cache.SetCacheability(HttpCacheability.NoCache);
    Response.Cache.SetNoStore(); Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1)); Response.Cache.SetMaxAge(TimeSpan.Zero); Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);

and in Fiddler I can see following headers:

Fiddler

  1. Adding OutputCache attributes on SomePage action method. - Didn't work

    [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*", Location = System.Web.UI.OutputCacheLocation.None)]

Fiddler

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]

enter image description here

Please advice, what else should be in this page header, which will instruct the browser - that it should not cache this page?

Upvotes: 1

Views: 1236

Answers (1)

Marcus
Marcus

Reputation: 453

You can globally turn caching off. Or by decorating the action in the controller

[OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")]
    Public ActionResult SomeApi(string param){
    ...
    }

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 [or not cached depending on the attribute values]. MSDN Article

Also of note: if your page actually has not changed, ASP.NET is smart enough to use a cached page for speed.


Updating after troubleshooting steps above tried. What about setting up your Ajax before the call.

$.ajaxSetup({ cache: false});

Upvotes: 1

Related Questions