【问题标题】:Cache.SetMaxAge not working under IIS, works fine under VS Dev SrvCache.SetMaxAge 在 IIS 下不工作,在 VS Dev Srv 下工作正常
【发布时间】:2011-01-07 14:59:29
【问题描述】:

我正在尝试在我的回复中添加一个“max-age”标题。它在我的 Visual Studio 开发服务器上运行良好,但是一旦我将应用程序移动到 IIS(在本地尝试 IIS express 和服务器上的 IIS) - 标题就会消失。

我的代码:

Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetMaxAge(new TimeSpan(1, 0, 0, 0));

VS 开发服务器响应(一切正常):

HTTP/1.1 200 OK
Server: ASP.NET Development Server/10.0.0.0
Date: Fri, 07 Jan 2011 14:55:04 GMT
X-AspNet-Version: 2.0.50727
Cache-Control: public, max-age=86400

IIS7 响应

HTTP/1.1 200 OK
Server: Microsoft-IIS/7.5
Date: Fri, 07 Jan 2011 15:00:54 GMT
X-AspNet-Version: 2.0.50727
Cache-Control: public

PS。这是一个 ASHX 处理程序,如果重要的话......

【问题讨论】:

    标签: c# asp.net iis browser-cache


    【解决方案1】:

    更新:2011-03-14 修复方法是确保您调用 SetSlidingExpiration(true)

    context.Response.Cache.SetCacheability(HttpCacheability.Public);
    context.Response.Cache.SetMaxAge(TimeSpan.FromMinutes(5));
    context.Response.ContentType = "image/jpeg";
    context.Response.Cache.SetSlidingExpiration(true);
    

    如果您删除 OutputCache 模块,您将获得所需的结果。我认为这是一个错误。

    因此,在您的 web.config 中,您将执行以下操作:

      <system.webServer>
          <modules runAllManagedModulesForAllRequests="true">
              <remove name="OutputCache"/>
          </modules>
      </system.webServer>
    

    添加:所以,还有其他信息。

    1. 使用MVC的OutputCacheAttribute显然没有这个问题
    2. 在同一个 MVC 应用程序下,不从模块中删除“OutputCache”,如果 IHttpHandler 或 ActionResult 导致 s-maxage 被剥离,则直接实现

    以下剥离 s-maxage

             public void ProcessRequest(HttpContext context)
        {
            using (var image = ImageUtil.RenderImage("called from IHttpHandler direct", 5, DateTime.Now))
            {
                context.Response.Cache.SetCacheability(HttpCacheability.Public);
                context.Response.Cache.SetMaxAge(TimeSpan.FromMinutes(5));
                context.Response.ContentType = "image/jpeg";
                image.Save(context.Response.OutputStream, ImageFormat.Jpeg);
            }            
        }
    

    以下剥离 s-maxage

              public ActionResult Image2()
        {
            MemoryStream oStream = new MemoryStream();
    
            using (Bitmap obmp = ImageUtil.RenderImage("Respone.Cache.Setxx calls", 5, DateTime.Now))
            {
                obmp.Save(oStream, ImageFormat.Jpeg);
                oStream.Position = 0;
                Response.Cache.SetCacheability(HttpCacheability.Public);
                Response.Cache.SetMaxAge(TimeSpan.FromMinutes(5));
                return new FileStreamResult(oStream, "image/jpeg");
            }
        }
    

    这不是 - 想一想......

        [OutputCache(Location = OutputCacheLocation.Any, Duration = 300)]
        public ActionResult Image1()
        {
            MemoryStream oStream = new MemoryStream();
    
            using (Bitmap obmp = ImageUtil.RenderImage("called with OutputCacheAttribute", 5, DateTime.Now))
            {
                obmp.Save(oStream, ImageFormat.Jpeg);
                oStream.Position = 0;
                return new FileStreamResult(oStream, "image/jpeg");
            }
        }
    

    【讨论】:

    • 谢谢!我发现除非设置了SetSlidingExpiration(true),否则响应标头中会省略max-age。同样,通过使用OutputCache,FWR 还会将Expires 标头添加到响应标头中(具有等效的 max-age 时间戳)。您的解决方案是我满足浏览器缓存的唯一方法。
    • 另外,对于读者,不要将[OutputCache] 属性和Response.Cache. 混用——这只会让情况变得更糟。只需操纵Response.Cache
    【解决方案2】:

    解决方案:

    web.config:

        <staticContent>
          <clientCache cacheControlCustom="public" cacheControlMode="UseMaxAge" cacheControlMaxAge="1.00:00:00"/>
        </staticContent>
    

    和 IIS PC:

    使用 cmd 转到 c:\windows\system32\inetsrv

    然后执行:

    appcmd unlock config /section:staticContent
    

    【讨论】:

      【解决方案3】:

      迟到的回答,但这可以帮助某人:-

      Response.Cache.SetProxyMaxAge(TimeSpan.Zero);
      

      【讨论】:

        猜你喜欢
        • 2021-06-29
        • 2022-01-01
        • 2015-11-19
        • 2012-07-13
        • 2014-12-09
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多