How to stop MVC caching the results of invoking an action method?

前端 未结 1 1200
逝去的感伤
逝去的感伤 2020-12-09 14:07

I am experiencing a problem with IE caching the results of an action method.

Other articles I found were related to security and the [Authorize] attribute. This

相关标签:
1条回答
  • 2020-12-09 14:27

    MVC doesn't cache the results. IE does.

    So you have to tell IE not to do it.

    Here's how I do it. First, an attribute:

    [AttributeUsage(AttributeTargets.Class | AttributeTargets.Method, Inherited = true, AllowMultiple = false)]
    public sealed class CacheControlAttribute : ActionFilterAttribute
    {
        public CacheControlAttribute(HttpCacheability cacheability)
        {
            this._cacheability = cacheability;
        }
    
        public HttpCacheability Cacheability { get { return this._cacheability; } } 
    
        private HttpCacheability _cacheability;
    
        public override void OnActionExecuted(ActionExecutedContext filterContext)
        {
            HttpCachePolicyBase cache = filterContext.HttpContext.Response.Cache;
            cache.SetCacheability(_cacheability);
        }
    }
    

    Next, an action:

        [CacheControl(HttpCacheability.NoCache), HttpGet]
        public JsonResult MyAction()
    
    0 讨论(0)
提交回复
热议问题