Cache.SetMaxAge 在 IIS 下不起作用,在 VS Dev Srv 下工作正常

发布于 2024-10-10 12:51:28 字数 691 浏览 7 评论 0原文

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

我的代码:

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

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

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 处理程序,如果重要的话......

I'm trying to add a "max-age" header to my response. It works fine on my Visual Studio Development Server, but as soon as I move the app to IIS (tried both IIS express locally and IIS on the server) - the header disappears.

My code:

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

VS Dev server response (all works just fine):

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 Response

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. It's an ASHX-handler, if it matters...

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

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

发布评论

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

评论(3

遗失的美好 2024-10-17 12:51:28

更新: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");
        }
    }

UPDATE: 2011-03-14 The fix is ensure you call 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);

If you remove the OutputCache module you will get the desired result. I see this as a bug.

So, in your web.config you would do the following:

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

ADDED: So, there's additional information.

  1. Using MVC's OutputCacheAttribute apparently doesn't have this issue
  2. Under the same MVC application, without removing "OutputCache" from the modules, a direct implementation if IHttpHandler or an ActionResult results in the s-maxage being stripped

The following strips the 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);
        }            
    }

The following strips the 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");
        }
    }

This does NOT - go figure...

    [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");
        }
    }
仙女 2024-10-17 12:51:28

解决方案:

web.config 中:

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

和 en IIS PC:

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

然后执行:

appcmd unlock config /section:staticContent

Solution:

in web.config:

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

and en IIS PC:

With cmd go to c:\windows\system32\inetsrv.

Then execute:

appcmd unlock config /section:staticContent
暗地喜欢 2024-10-17 12:51:28

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

Response.Cache.SetProxyMaxAge(TimeSpan.Zero);

Belated answered but this could help someone :-

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