为了确保JQuery不会缓存结果,请在您的ajax方法上放置以下内容:
$.ajax({ cache: false //rest of your ajax setup});为了防止在MVC中进行缓存,我们创建了自己的属性,您可以这样做。这是我们的代码:
[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); }}然后只需使用来装饰您的控制器
[NoCache]。或为所有目的做到这一点,您只需将属性放在继承您的控制器(如果有)的基类的类上,就像我们在这里:
[NoCache]public class Controllerbase : Controller, IControllerbase
如果您需要某些动作不可缓存,则也可以用此属性来装饰某些动作,而不是装饰整个控制器。
如果您的类或动作
NoCache在浏览器中呈现时没有,并且您想检查它是否正常工作,请记住,编译更改后,您需要在浏览器中执行“硬刷新”(Ctrl +
F5)。在执行此操作之前,浏览器将保留旧的缓存版本,并且不会使用“正常刷新”(F5)刷新它。



