17

我正在尝试在我的回复中添加一个“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 处理程序,如果重要的话......

4

3 回答 3

31

更新: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”,直接实现 if 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");
        }
    }
于 2011-03-11T17:31:19.167 回答
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
于 2011-10-01T10:15:23.577 回答
0

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

Response.Cache.SetProxyMaxAge(TimeSpan.Zero);
于 2016-11-04T10:07:40.270 回答