2

I activate caching on my Visual Studio ASP.NET MVC4 project.

Here is the portion of code where I manage caching:

filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(1));
filterContext.HttpContext.Response.Cache.SetValidUntilExpires(true);
filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.Public);

For testing I placed a breakpoint in the action Edit page. When I run my application and navigate to a specific page (an editing page) I can easily see that the action page is cached because the breakpoint is never reached. Next I cleared the cache with CTRL+F5 and navigate to the same Edit page. I expect to reach the breakpoint in my action Edit page but this is not the case. I never hit the Edit action page, never!?

enter image description here

enter image description here

Any idea why?

Thanks.

Bronzato
  • 9,438
  • 29
  • 120
  • 212

1 Answers1

0

Have you tried using the OutputCache attribute instead? It is simpler to control (done through config) and easier to use that writing to the response.

Example basic usage:

[OutputCache(Duration=10, VaryByParam="none")]
public ActionResult Index()
{
    return View();
}

Or you can create your own named settings in web.config. Example config:

<caching>
    <outputCacheSettings>
        <outputCacheProfiles>
            <add name="Cache1Hour" duration="3600" varyByParam="none"/>
        </outputCacheProfiles>
    </outputCacheSettings>
</caching>

...and used like this:

Example usage:

[OutputCache(CacheProfile="Cache1Hour")]
public ActionResult Index()
{
    return View();
}

Examples copied from here

MarkG
  • 1,859
  • 1
  • 19
  • 21