2011-09-06 64 views
1

在我的ASP.NET MVC 2应用程序中,我已经使用下列属性装饰了一个动作。ASP.NET MVC - web.config中的高速缓存和压缩设置不总是被使用

[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的标头,我receieve一个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,但确实有“缓存控制”和“过期”字段设置。

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

我最终发现,这个问题是由我的主机服务器公司配置IIS导致的,只有在请求页面时才通过Etag 更多比每秒两次。

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