To ensure that JQuery isn't caching the results, on your ajax methods, put the following:
$.ajax({
cache: false
//rest of your ajax setup
});
Or to prevent caching in MVC, we created our own attribute, you could do the same. Here's our code:
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public sealed class NoCacheAttribute : ActionFilterAttribute
{
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1));
filterContext.HttpContext.Response.Cache.SetValidUntilExpires(false);
filterContext.HttpContext.Response.Cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
filterContext.HttpContext.Response.Cache.SetCacheability(HttpCacheability.NoCache);
filterContext.HttpContext.Response.Cache.SetNoStore();
base.OnResultExecuting(filterContext);
}
}
Then just decorate your controller with [NoCache]
. OR to do it for all you could just put the attribute on the class of the base class that you inherit your controllers from (if you have one) like we have here:
[NoCache]
public class ControllerBase : Controller, IControllerBase
You can also decorate some of the actions with this attribute if you need them to be non-cacheable, instead of decorating the whole controller.
If your class or action didn't have NoCache
when it was rendered in your browser and you want to check it's working, remember that after compiling the changes you need to do a "hard refresh" (Ctrl+F5) in your browser. Until you do so, your browser will keep the old cached version, and won't refresh it with a "normal refresh" (F5).
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…