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

C# 如何在asp mvc中清除指定控制器中的缓存?,c#,asp.net,.net,asp.net-mvc,caching,C#,Asp.net,.net,Asp.net Mvc,Caching,可能重复: 如何清除指定控制器中的缓存 我尝试使用几种方法: Response.RemoveOutputCacheItem(); Response.Cache.SetExpires(DateTime.Now); 没有任何效果,它不起作用( 是否有任何方法可以获取控制器缓存中的所有密钥并显式删除它们? 我应该在哪个被重写的方法中执行清除缓存?以及如何执行 有什么想法吗?试试这个: 把这个放在你的模型上: public class NoCache : ActionFilterAttribute

可能重复:

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

我尝试使用几种方法:

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);
    }
}
在您的特定控制器上: e、 g:

来源:

你试过了吗

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

}
如果这不适合您,那么Mark Yu建议使用自定义属性。

试试这个:

public void ClearApplicationCache()
{
    List<string> keys = new List<string>();

    // 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]);
    }
}
public void ClearApplicationCache()
{
列表键=新列表();
//检索应用程序缓存枚举器
IDictionaryEnumerator enumerator=Cache.GetEnumerator();
//复制缓存中当前存在的所有密钥
while(枚举数.MoveNext())
{
Add(enumerator.Key.ToString());
}
//从缓存中删除每个密钥
对于(int i=0;i
这是可行的,但不是立即生效,需要等待最后一个缓存何时过期。在我的场景中,这对我很有效……非常感谢。。
public void ClearApplicationCache()
{
    List<string> keys = new List<string>();

    // 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]);
    }
}