Disable browser cache for entire ASP.NET website

前端 未结 8 1644
既然无缘
既然无缘 2020-11-22 03:20

I am looking for a method to disable the browser cache for an entire ASP.NET MVC Website

I found the following method:

Response.Cach         


        
8条回答
  •  情书的邮戳
    2020-11-22 03:58

    Create a class that inherits from IActionFilter.

    public 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 put attributes where needed...

    [NoCache]
    [HandleError]
    public class AccountController : Controller
    {
        [NoCache]
        [Authorize]
        public ActionResult ChangePassword()
        {
            return View();
        }
    }
    

提交回复
热议问题