代码改变世界

ASP.NET MVC 扩展之 NoCacheAttribute

2012-05-21 18:34  音乐让我说  阅读(528)  评论(0编辑  收藏  举报

ASP.NET MVC 扩展之 NoCacheAttribute 是为了让 Action 方法不缓存,直接贴代码了:

public class NoCacheAttribute : FilterAttribute, IActionFilter
{
    public void OnActionExecuted(ActionExecutedContext filterContext)
    {

    }

    public void OnActionExecuting(ActionExecutingContext filterContext)
    {
        filterContext.HttpContext.Response.Cache.SetExpires(DateTime.UtcNow.AddDays(-1.0));
        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();
    }
}

 

 

谢谢浏览!