如何在asp mvc中清除指定控制器中的缓存?

可能重复:
如何以编程方式清除控制器操作方法的outputcache

如何清除指定控制器中的缓存?

我尝试使用几种方法:

Response.RemoveOutputCacheItem(); Response.Cache.SetExpires(DateTime.Now); 

没有任何影响,它不起作用。 :(可能存在以任何方式获取控制器缓存中的所有键并明确删除它们?在哪个重写方法我应该执行清除缓存?以及如何做到这一点?

有什么想法吗?

试试这个:

把它放在你的模型上:

 public class NoCache : 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] [Authorize] public ActionResult Home() { ////////... } 

来源: 原始代码

你有没有尝试过

 [OutputCache(NoStore = true, Duration = 0, VaryByParam = "*")] public ActionResult DontCacheMeIfYouCan() { } 

如果这不适合你,那么Mark Yu建议的自定义属性。

试试这个 :

 public void ClearApplicationCache() { List keys = new List(); // retrieve application Cache enumerator IDictionaryEnumerator enumerator = Cache.GetEnumerator(); // copy all keys that currently exist in Cache while (enumerator.MoveNext()) { keys.Add(enumerator.Key.ToString()); } // delete every key from cache for (int i = 0; i < keys.Count; i++) { Cache.Remove(keys[i]); } }