新加载页面而不是缓存

pra*_*ech 2 browser-cache asp.net-mvc-3

有没有办法可以强制页面不从缓存加载?每次加载页面时都会从服务器加载.我正在使用asp.net MVC 3.

Dar*_*rov 12

您可以使用自定义无缓存操作筛选器:

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

然后使用您不希望被客户端浏览器缓存的此属性装饰任何控制器/操作.