MVC4捆绑缓存头

时间:2012-10-25 09:15:52

标签: c# asp.net asp.net-mvc-4 asp.net-optimization bundling-and-minification

我想更改捆绑请求发送的缓存标头。目前它的变化是User-Agent,但我不希望它,是否有办法更改捆绑请求发送的标头?

快速查看System.Web.Optimization程序集后,我可以看到标题设置在Bundle.SetHeaders这是一个私有静态函数,所以我不认为它可能,尽管我希望被证明是错误的

2 个答案:

答案 0 :(得分:10)

这不是我们今天公开的内容。我们只在BundleResponse上暴露了IBundleTransform可能改变的Cacheability属性。是的,我们明确地设置了以下内容:

                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;

我们有一个工作项我们的积压工具可以打开它,并使其在未来更具可扩展性/可定制性。

答案 1 :(得分:0)

janv8000's comment on this response中所述,有一种解决方法。您需要将以下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>

显然,您需要注意将所有捆绑包放在捆绑包文件夹中,或者相应地更改IsBundles前提条件。

相关问题