【发布时间】:2017-08-08 15:21:14
【问题描述】:
所以我在我的 Windows 10 笔记本电脑上本地运行 IIS 10 上的 ASP.NET MVC 5 站点,在 web.config 中具有以下设置。
在Release配置中编译,关闭调试模式:
<compilation debug="false" targetFramework="4.5.2" />
以及使用 max-age 的 Cache-Control 标头:
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="365.00:00:00" />
</staticContent>
但无论我做什么,我总是看到 Cache-Control: private
HTTP/1.1 200 OK
Cache-Control: private
Content-Type: text/html; charset=utf-8
我已经关闭了我拥有的所有模块并运行了一个失败的跟踪请求,这表明正在设置标头:
但我无法找到关于这个 ManagedPipelineHandler 的任何信息,它似乎是设置标头的模块。
我尝试将 Cache-Control 添加为自定义标头:
<httpProtocol>
<customHeaders>
<remove name="X-Powered-By" />
<remove name="Cache-Control" />
<add name="Cache-Control" value="public, max-age=1800, must-revalidate" />
</customHeaders>
</httpProtocol>
但这只是附加到现有的私有设置中:
HTTP/1.1 200 OK
Cache-Control: private,public, max-age=1800, must-revalidate
Content-Type: text/html; charset=utf-8
我的 runAllManagedModulesForAllRequests 设置为 false,其他帖子认为这可能是原因。
我知道我可以在代码中设置此标头,但我很想知道强制“私有”设置的原因和原因。
谁能给点建议?
更新
Something is forcing responses to have cache-control: private in IIS7
因此,当没有用于请求的输出缓存(并且您启用了输出缓存)时,这是 .NET 的默认行为。如果在 web.config 中将 & 的 sendCacheControlHeader 属性设置为 false - 您不会获得 Cache-Control: private 标头!
奇怪的是,您仍然没有基于节点获得正确的 Cache-Control 标头 - 但您通过该部分控制标头,所以现在可以使用:
<httpProtocol>
<customHeaders>
<remove name="X-Powered-By" />
<add name="Cache-Control" value="max-age=30,public" />
</customHeaders>
</httpProtocol>
【问题讨论】:
-
你能解决这个问题吗?我有相同的问题。我想为 WebApi 方法返回不同的缓存控制标头,以便可以在 CDN 上缓存结果。
标签: c# asp.net asp.net-mvc caching