代码之家  ›  专栏  ›  技术社区  ›  Marek

如何指定HTTP到期头(ASP.NET(MVC+IIS)

  •  52
  • Marek  · 技术社区  · 16 年前

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

    Page speed 告诉我在响应头中为css和图像指定HTTP缓存过期时间。

    我的代码:

    Response.Expires
    Response.ExpiresAbsolute
    Response.CacheControl
    

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

    问题是 如何为自动提供的资源(如图像、css等)设置Expires标头?

    3 回复  |  直到 12 年前
        1
  •  79
  •   Marek    16 年前

    找到了:

    我需要为静态内容指定客户端缓存(在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/clientCache

        2
  •  31
  •   Drew Noakes    15 年前

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

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

        3
  •  2
  •   dariol    16 年前
    推荐文章