解决方法
创建全局操作过滤器并覆盖OnResultExecuting():
public class DisableCache : 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(); } }
然后在你的global.asax中注册,像这样:
public static void RegisterGlobalFilters(GlobalFilterCollection filters) { filters.Add(new DisableCache()); }
总而言之,这样做是创建一个Global Action Filter,所以这将隐含地应用于所有控制器和所有操作.