2011-06-06 44 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