简体   繁体   English

MVC4捆绑缓存头

[英]MVC4 Bundling Cache Headers

I want to change the cache headers sent from a bundle request. 我想更改从捆绑请求发送的缓存标头。 Currently it is varying by User-Agent but I don't want it to, is there a way to change the headers sent by a bundle request? 目前它是由User-Agent改变但我不希望它,有没有办法更改捆绑请求发送的标头?

After a quick look in the System.Web.Optimization assembly I can see the headers get set in Bundle.SetHeaders which is a private static function so I don't think its possible although I would love to be proven wrong. 在快速查看System.Web.Optimization程序集后,我可以看到标题在Bundle.SetHeaders中设置,这是一个私有静态函数,所以我不认为它可能,尽管我希望被证明是错误的。

This isn't something that we currently expose today. 这不是我们目前公开的内容。 We only expose the Cacheability property on the BundleResponse that a IBundleTransform could change. 我们只在BundleResponse上暴露了IBundleTransform可能改变的Cacheability属性。 And yes we explicitly set the following things: 是的,我们明确地设置了以下内容:

                HttpCachePolicyBase cachePolicy = context.HttpContext.Response.Cache;
                cachePolicy.SetCacheability(bundleResponse.Cacheability);
                cachePolicy.SetOmitVaryStar(true);
                cachePolicy.SetExpires(DateTime.Now.AddYears(1));
                cachePolicy.SetValidUntilExpires(true);
                cachePolicy.SetLastModified(DateTime.Now);
                cachePolicy.VaryByHeaders["User-Agent"] = true;

We have a work item our backlog to open this up and make this more extensible/customizable in the future. 我们有一个工作项目我们的积压工具打开它,并使其在未来更具可扩展性/可定制性。

There is a workaround around it as mentioned in janv8000's comment on this response . 正如janv8000对此响应的评论中提到的那样,有一个解决方法。 You need to add the following URL rewrite rule to your web server: 您需要将以下URL重写规则添加到Web服务器:

<system.webServer>
    <rewrite>
        <outboundRules>
            <rule name="Cache Bundles" preCondition="IsBundles" patternSyntax="ExactMatch">
                <match serverVariable="RESPONSE_Vary" pattern="User-Agent" />
                <action type="Rewrite" value="Accept-Encoding" />
            </rule>
            <preConditions>
                <preCondition name="IsBundles" patternSyntax="Wildcard">
                    <add input="{URL}" pattern="*/bundles/*" />
                </preCondition>
            </preConditions>
        </outboundRules>
    </rewrite>
</system.webServer>

Obviously you need to pay attention to have all your bundles in your bundles folder or change the IsBundles precondition accordingly. 显然,您需要注意将所有捆绑包放在捆绑包文件夹中或相应地更改IsBundles前提条件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM