ASP.NET MVC - 缓存和web.config 中的压缩设置并不总是被使用
在我的 ASP.NET MVC 2 应用程序中,我使用以下属性修饰了一个操作。
[ETag]
[OutputCache(Duration = 604800, VaryByParam = "none", Location = OutputCacheLocation.Any)]
public ActionResult Index()
(对于 ETag 属性,我使用了此处找到的属性类.)
我还将以下内容添加到我的 web.config 中。
<!-- gzip compression -->
<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
<scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" staticCompressionLevel="9" />
<dynamicTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/x-javascript" enabled="true" />
<add mimeType="application/json" enabled="true" />
<add mimeType="*/*" enabled="false" />
</dynamicTypes>
<staticTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/x-javascript" enabled="true" />
<add mimeType="application/atom+xml" enabled="true" />
<add mimeType="application/xaml+xml" enabled="true" />
<add mimeType="*/*" enabled="false" />
</staticTypes>
</httpCompression>
<urlCompression doStaticCompression="true" doDynamicCompression="true" />
<!-- ETags -->
<httpProtocol>
<customHeaders>
<add name="ETag" value="""" />
</customHeaders>
</httpProtocol>
<!-- Expiry -->
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" />
</staticContent>
我的问题是,当我从托管服务器加载页面并在 Firebug 中查看标头时,我收到了 ETag,但没有缓存过期。
Cache-Control private
Content-Type text/html; charset=utf-8
Etag HKQ9Pp6hFTMRZYZbbIi3HA==,""
Server Microsoft-IIS/7.0
X-AspNetMvc-Version 2.0
X-AspNet-Version 4.0.30319
X-Powered-By ASP.NET
Date Tue, 06 Sep 2011 18:31:05 GMT
Content-Length 4447
当我在本地加载页面时,响应没有有 ETag,但有设置了“Cache-Control”和“Expires”字段。
Server ASP.NET Development Server/10.0.0.0
Date Tue, 06 Sep 2011 18:33:55 GMT
X-AspNet-Version 4.0.30319
X-AspNetMvc-Version 2.0
Cache-Control private, max-age=604800
Expires Tue, 13 Sep 2011 18:33:54 GMT
Last-Modified Tue, 06 Sep 2011 18:33:54 GMT
Content-Type text/html; charset=utf-8
Content-Length 4447
Connection Close
另请注意,虽然我在 web.config 中设置 gzip 压缩,但它不会出现在响应标头中。
那么为什么我的 web.config 文件中的设置有时会被忽略呢?我的托管服务器是否有可能覆盖这些?如果是这样,那么他们删除缓存或压缩设置就没有多大意义,不是吗?
In my ASP.NET MVC 2 application I've decorated an action with the following attributes.
[ETag]
[OutputCache(Duration = 604800, VaryByParam = "none", Location = OutputCacheLocation.Any)]
public ActionResult Index()
(for the ETag attribute I've used the attribute class found here.)
I've also added the following to my web.config.
<!-- gzip compression -->
<httpCompression directory="%SystemDrive%\inetpub\temp\IIS Temporary Compressed Files">
<scheme name="gzip" dll="%Windir%\system32\inetsrv\gzip.dll" staticCompressionLevel="9" />
<dynamicTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/x-javascript" enabled="true" />
<add mimeType="application/json" enabled="true" />
<add mimeType="*/*" enabled="false" />
</dynamicTypes>
<staticTypes>
<add mimeType="text/*" enabled="true" />
<add mimeType="message/*" enabled="true" />
<add mimeType="application/x-javascript" enabled="true" />
<add mimeType="application/atom+xml" enabled="true" />
<add mimeType="application/xaml+xml" enabled="true" />
<add mimeType="*/*" enabled="false" />
</staticTypes>
</httpCompression>
<urlCompression doStaticCompression="true" doDynamicCompression="true" />
<!-- ETags -->
<httpProtocol>
<customHeaders>
<add name="ETag" value="""" />
</customHeaders>
</httpProtocol>
<!-- Expiry -->
<staticContent>
<clientCache cacheControlMode="UseMaxAge" cacheControlMaxAge="7.00:00:00" />
</staticContent>
My problem is that when I load the page from my hosting server and view the header in Firebug I receieve an ETag, but no cache expiry.
Cache-Control private
Content-Type text/html; charset=utf-8
Etag HKQ9Pp6hFTMRZYZbbIi3HA==,""
Server Microsoft-IIS/7.0
X-AspNetMvc-Version 2.0
X-AspNet-Version 4.0.30319
X-Powered-By ASP.NET
Date Tue, 06 Sep 2011 18:31:05 GMT
Content-Length 4447
When I load the page locally the response does not have an ETag, but does have "Cache-Control" and "Expires" fields set.
Server ASP.NET Development Server/10.0.0.0
Date Tue, 06 Sep 2011 18:33:55 GMT
X-AspNet-Version 4.0.30319
X-AspNetMvc-Version 2.0
Cache-Control private, max-age=604800
Expires Tue, 13 Sep 2011 18:33:54 GMT
Last-Modified Tue, 06 Sep 2011 18:33:54 GMT
Content-Type text/html; charset=utf-8
Content-Length 4447
Connection Close
Also, note that although I'm setting gzip compression in the web.config, it doesn't appear in either of the response headers.
So why do the settings in my web.config file appear to be ignored part of the time? Is it possible that my hosting server overrides these? If so, it doesn't make much sense for them to remove the cache or compression settings, does it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我最终发现这个问题是由于我的主机服务器公司将 IIS 配置为仅在每秒请求页面超过两次的情况下传递 Etag。
因此,上面的代码通常可以工作,但是在离开服务器时一些信息被删除,这似乎是错误的做法,但它解释了发生的情况。
I eventually found that this problem was being caused by my host server company configuring IIS to only pass the Etag if a page is requested more than twice-a-second.
Therefore the code above would normally work but some information was being removed when leaving the server, which seems the wrong thing to do but it explains what was happening.