我想更改从捆绑请求发送的缓存标头。目前它有所不同,User-Agent
但我不希望这样,有没有办法更改捆绑请求发送的标头?
在快速查看System.Web.Optimization
程序集后,我可以看到标题设置在Bundle.SetHeaders
其中是一个私有静态函数,所以我认为它不可能,尽管我很想被证明是错误的。
我想更改从捆绑请求发送的缓存标头。目前它有所不同,User-Agent
但我不希望这样,有没有办法更改捆绑请求发送的标头?
在快速查看System.Web.Optimization
程序集后,我可以看到标题设置在Bundle.SetHeaders
其中是一个私有静态函数,所以我认为它不可能,尽管我很想被证明是错误的。
这不是我们今天公开的事情。我们只在 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;
我们有一个待办事项的工作项目来打开它,并在未来使其更具可扩展性/可定制性。
如janv8000 对此响应的评论中所述,有一个解决方法。您需要将以下 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
相应地更改前提条件。