让Asp.net/IIS为静态文件设置Cache-control:max-age

20

我们有一个使用URL路由的Webforms项目。我已经定义了图片和CSS文件的异常路由,如下:

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

静态文件应该直接由IIS提供服务,并绕过路由。

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

2个回答

30

解决方案是在 web.config 文件中使用 system.webserver 部分来配置服务器缓存(和压缩)。这里是一个起点: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>

无法正常工作,不确定原因 https://stackoverflow.com/questions/54549204/asp-net-core-2-0-website-cache-control-not-working-as-expected - Jitendra Pancholi

16

Dario的回答让我受益匪浅,但我不得不向<clientCache>添加一个属性:cacheControlCustom="public",否则IIS将不会向浏览器发送Cache-Control头信息。请参见此答案


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接