asp.net-mvc 如何指定 HTTP 过期标头?(ASP.NET MVC+IIS)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2608739/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to specify HTTP expiration header? (ASP.NET MVC+IIS)
提问by Marek
I am already using output caching in my ASP.NET MVC application.
我已经在我的 ASP.NET MVC 应用程序中使用了输出缓存。
Page speedtells me to specify HTTP cache expiration for css and images in the response header.
页面速度告诉我在响应头中为 css 和图像指定 HTTP 缓存过期时间。
I know that the Response object contains some properties that control cache expiration. I know that these properties can be used to control HTTP caching for response that I am serving from my code:
我知道 Response 对象包含一些控制缓存过期的属性。我知道这些属性可用于控制我从我的代码提供的响应的 HTTP 缓存:
Response.Expires
Response.ExpiresAbsolute
Response.CacheControl
or alternatively
或者
Response.AddHeader("Expires", "Thu, 01 Dec 1994 16:00:00 GMT");
The question is how do I set the Expires header for resources that are served automatically, e.g. images, css and such?
问题是如何为自动提供的资源(例如图像、CSS 等)设置 Expires 标头?
回答by Marek
Found it:
找到了:
I need to specify client cache for static content (in web.config).
我需要为静态内容指定客户端缓存(在 web.config 中)。
<configuration>
<system.webServer>
<staticContent>
<clientCache cacheControlCustom="public"
cacheControlMaxAge="12:00:00" cacheControlMode="UseMaxAge" />
</staticContent>
</system.webServer>
</configuration>
from http://www.iis.net/ConfigReference/system.webServer/staticContent/clientCache
来自http://www.iis.net/ConfigReference/system.webServer/staticContent/clientCache
回答by Drew Noakes
If you want to do it from code for a resource that you're returning (ie. not a static file being served from IIS), you're better off using Response.Cache
:
如果您想从要返回的资源的代码(即不是从 IIS 提供的静态文件)中执行此操作,最好使用Response.Cache
:
Response.Cache.SetExpires(DateTime.Now.AddYears(1));
Response.Cache.SetCacheability(HttpCacheability.Public);
I know that's not exactly what you're asking for, but I found this question via Google and figure others might like this answer as it's related to the APIs you show in the original question text.
我知道这不完全是您所要求的,但是我通过 Google 发现了这个问题,并认为其他人可能会喜欢这个答案,因为它与您在原始问题文本中显示的 API 相关。
回答by dariol
Look at mini static content deliveryproject. :)
看看迷你静态内容交付项目。:)