How to invalidate cache data [OutputCache] from a Controller?

后端 未结 2 1667
被撕碎了的回忆
被撕碎了的回忆 2020-12-04 19:58

Using ASP.Net MVC 3 I have a Controller which output is being cached using attributes [OutputCache]

[OutputCache]
public controllerA(){}
         


        
2条回答
  •  青春惊慌失措
    2020-12-04 20:12

    You can do that by using a custom attribute, like so:

    [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 on your controllerb you can do:

    [NoCache]
    public class controllerB
    {
    }
    

提交回复
热议问题