获取Asp.net/iis为静态文件设置Cache-control:max-age
我们有一个带有 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上运行。
We have a Webforms project with url routing. I have defined exception routes for images and css-files as
routes.Add("IgnoreImages", new Route("img/{*pathInfo}", new StopRoutingHandler()));
routes.Add("IgnoreCss", new Route("css/{*pathInfo}", new StopRoutingHandler()));
so static files should be served by IIS directly and routing should be bypassed.
When checking the response for an image with Fiddler the only key under the Cache heading is Date. What's missing is the Cache-control:max:age key. How can I specify caching policy for static files? The application is run on IIS7.5.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
解决方案是使用 web.config 文件中的 system.webserver 部分来配置服务器缓存(和压缩)。这是一个起点: http://www.iis.net/ConfigReference/system .webServer/staticContent/clientCache
示例:
The solution is using the
system.webserver
section in the web.config file to configure server caching (and compression). Here is a starting point: http://www.iis.net/ConfigReference/system.webServer/staticContent/clientCacheExample:
Dario 的回答让我大部分时间都明白了,但我必须向
添加一个属性,cacheControlCustom="public"
,否则 IIS 不会发送 Cache-Control标头到浏览器。请参阅此答案。Dario's answer got me most of the way but I had to add an attribute to
<clientCache>
,cacheControlCustom="public"
, otherwise IIS did not send the Cache-Control header to the browser. See this answer.