【问题标题】:MVC4 Bundling Cache HeadersMVC4 捆绑缓存头
【发布时间】:2012-10-15 10:39:51
【问题描述】:

我想更改从捆绑请求发送的缓存标头。目前它与User-Agent 不同,但我不希望这样,有没有办法更改捆绑请求发送的标头?

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

【问题讨论】:

  • 我也对任何人对首先添加此标头的逻辑的任何想法感兴趣 - 例如捆绑代码本身的行为是否因用户代理而异?还是只是预防措施?
  • 关于我们为什么这样做,它需要 VS Page Inspector 功能,该功能使用 IE 发送自定义用户代理,以防止 IE 的缓存弄乱对页面检查器或服务页面的请求检查器捆绑到 VS 之外的 IE。
  • @HaoKung 我有同样的问题,我希望能够将缓存控制标头值设置为public,max-age=31536000 而不是private,并添加一个 Etag 标头。看起来这两个目前都不可能?

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


【解决方案1】:

这不是我们今天公开的内容。我们只在 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;

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

【讨论】:

  • 是否有任何迹象表明此更改的时间表,此标头的影响对 CDN 集成造成严重破坏
  • 问题跟踪网址:aspnetoptimization.codeplex.com/workitem/136。还提到了一种可能的解决方法。
  • 已经快六年了。解决这个问题有什么进展吗?
【解决方案2】:

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 前提条件。

【讨论】:

    猜你喜欢
    • 2012-08-21
    • 2012-06-21
    • 2013-04-02
    • 2012-07-22
    • 1970-01-01
    • 2012-06-28
    • 2014-09-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多