load page freshly and not from cache

后端 未结 1 1203
余生分开走
余生分开走 2021-01-03 14:21

Is there way I can force page not to load from cache? Everytime page is loaded it is loaded from server. I\'m using asp.net MVC 3.

相关标签:
1条回答
  • 2021-01-03 15:11

    You could use a custom no cache action filter:

    public class NoCacheAttribute : ActionFilterAttribute
    {  
        public override void OnResultExecuting(ResultExecutingContext filterContext)
        {
            var cache = filterContext.HttpContext.Response.Cache;
            cache.SetExpires(DateTime.UtcNow.AddDays(-1));
            cache.SetValidUntilExpires(false);
            cache.SetRevalidation(HttpCacheRevalidation.AllCaches);
            cache.SetCacheability(HttpCacheability.NoCache);
            cache.SetNoStore();
            base.OnResultExecuting(filterContext);
        }
    }
    

    Then decorate any controller/action with this attribute that you don't want to be cached by client browsers.

    0 讨论(0)
提交回复
热议问题