如何指定 HTTP 过期标头? (ASP.NET MVC+IIS)

发布于 2024-08-29 00:55:59 字数 477 浏览 3 评论 0原文

我已经在 ASP.NET MVC 应用程序中使用输出缓存。

页面速度告诉我在响应标头中指定 CSS 和图像的 HTTP 缓存过期时间。

我知道 Response 对象包含一些控制缓存过期的属性。我知道这些属性可用于控制我从代码中提供的响应的 HTTP 缓存:

Response.Expires
Response.ExpiresAbsolute
Response.CacheControl

或者

Response.AddHeader("Expires", "Thu, 01 Dec 1994 16:00:00 GMT");

问题是如何为自动提供的资源设置 Expires 标头,例如图像、CSS 等?

I am already using output caching in my ASP.NET MVC application.

Page speed tells me to specify HTTP cache expiration for css and images in the response header.

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.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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

染柒℉ 2024-09-05 00:55:59

找到它:

我需要为静态内容指定客户端缓存(在 web.config 中)。

<configuration>
  <system.webServer>
    <staticContent>
      <clientCache cacheControlCustom="public" 
      cacheControlMaxAge="12:00:00" cacheControlMode="UseMaxAge" />
    </staticContent>
   </system.webServer>
</configuration>

来自 http://www.iis.net/ConfigReference/system.webServer/staticContent/客户端缓存

Found it:

I need to specify client cache for static content (in 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

白衬杉格子梦 2024-09-05 00:55:59

如果您想从要返回的资源的代码中执行此操作(即不是从 IIS 提供的静态文件),那么最好使用 Response.Cache:

Response.Cache.SetExpires(DateTime.Now.AddYears(1));
Response.Cache.SetCacheability(HttpCacheability.Public);

我知道这不是正是您所要求的,但我通过 Google 找到了这个问题,并认为其他人可能会喜欢这个答案,因为它与您在原始问题文本中显示的 API 相关。

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:

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文