2011-06-06 45 views
11

我们有一个带有URL路由的Webforms项目。我已经为图像和CSS-文件中定义的异常路线为Get Asp.net/iis设置Cache-control:静态文件的最大年龄

routes.Add("IgnoreImages", new Route("img/{*pathInfo}", new StopRoutingHandler())); 
routes.Add("IgnoreCss", new Route("css/{*pathInfo}", new StopRoutingHandler())); 

所以静态文件应该由IIS直接送达和路由应该被忽略。

使用Fiddler检查图像的响应时,缓存标题下的唯一键为Date。缺少的是Cache-control:max:age键。如何为静态文件指定缓存策略?该应用程序在IIS7.5上运行。

回答

21

该解决方案使用web.config文件中的​​部分来配置服务器缓存(和压缩)。这里是一个起点:http://www.iis.net/ConfigReference/system.webServer/staticContent/clientCache

例子:

<configuration> 
    <system.webServer> 
    <staticContent> 
     <clientCache cacheControlMode="UseMaxAge" 
     cacheControlMaxAge="1.00:00:00" /> <!-- 1 day --> 
    </staticContent> 
    </system.webServer> 
</configuration> 
11

达里奥的回答让我最的方式,但我不得不将属性添加到<clientCache>cacheControlCustom="public",否则IIS没有发送Cache-Control头到浏览器。见this answer