MVC缓存缩小捆绑包在新版本

时间:2016-10-25 15:12:45

标签: asp.net-mvc caching browser-cache bundling-and-minification

我们的ASP.NET MVC站点使用CSS / JS捆绑(System.Web.Optimization版本1.1.3)。我们在推出过程中遇到了问题:

当我在发布后发出第一个请求(使用地址栏 - >输入)时,我看到了捆绑文件的陈旧本地缓存版本。标题是:

dev tools cache directives

在确定是否可以提供缓存版本之前,我希望有一个304响应来检查服务器上的实际文件,而不是200。有没有办法这样做而不必使用像ctrl-f5或ctrl-r这样的特殊命令?

1 个答案:

答案 0 :(得分:0)

创建一个属性,如:

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
public sealed 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);
    }
}

之后,您可以将控制器置于控制器之上以防止缓存。实施例

[NoCache]
public HomeController : Controller
{
    // ....
}

或者只是在控制器上使用它。

[OutputCache(NoStore = true, Duration = 0)]