ASP.NET MVC - Cache& web.config中的压缩设置并不总是被使用

时间:2011-09-06 18:55:01

标签: asp.net-mvc caching web-config etag http-compression

在我的ASP.NET MVC 2应用程序中,我使用以下属性修饰了一个动作。

[ETag]
[OutputCache(Duration = 604800, VaryByParam = "none", Location = OutputCacheLocation.Any)]
public ActionResult Index()

(对于ETag属性,我使用了here找到的属性类。)

我还在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="&quot;&quot;" />
  </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文件中的设置在某些时候似乎被忽略了?我的托管服务器是否有可能覆盖这些?如果是这样,对于他们删除缓存或压缩设置没有多大意义,是吗?

1 个答案:

答案 0 :(得分:1)

我最终发现这个问题是由我的主机服务器公司将IIS配置为仅在请求页面时传递Etag more 而不是每秒两次。

因此,上面的代码通常可以正常工作,但是一些信息在离开服务器时被删除了,这似乎是错误的,但它解释了发生了什么。