【问题标题】:Get Asp.net/iis to set Cache-control:max-age for static files获取 Asp.net/iis 为静态文件设置 Cache-control:max-age
【发布时间】:2011-09-09 05:20:25
【问题描述】:

我们有一个带有 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 上运行。

【问题讨论】:

    标签: asp.net image caching cache-control


    【解决方案1】:

    解决方案是使用 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>
    

    【讨论】:

    【解决方案2】:

    Dario 的回答让我得到了大部分的帮助,但我必须向&lt;clientCache&gt;cacheControlCustom="public" 添加一个属性,否则 IIS 不会将 Cache-Control 标头发送到浏览器。见this answer

    【讨论】:

      猜你喜欢
      • 2011-08-22
      • 2017-03-29
      • 1970-01-01
      • 2010-10-14
      • 1970-01-01
      • 1970-01-01
      • 2012-07-06
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多