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

Ale*_*lex 15 c# asp.net iis browser-cache

我正在尝试为我的回复添加"max-age"标题.它在我的Visual Studio开发服务器上工作正常,但只要我将应用程序移动到IIS(尝试本地IIS表达和服务器上的IIS) - 标题就会消失.

我的代码:

Response.Cache.SetCacheability(HttpCacheability.Public);
Response.Cache.SetMaxAge(new TimeSpan(1, 0, 0, 0));
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

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
Run Code Online (Sandbox Code Playgroud)

PS.它是一个ASHX处理程序,如果它很重要......

Sha*_*ria 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);
Run Code Online (Sandbox Code Playgroud)

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

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

  <system.webServer>
      <modules runAllManagedModulesForAllRequests="true">
          <remove name="OutputCache"/>
      </modules>
  </system.webServer>
Run Code Online (Sandbox Code Playgroud)

增加:所以,还有其他信息.

  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);
        }            
    }
Run Code Online (Sandbox Code Playgroud)

以下剥离了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");
        }
    }
Run Code Online (Sandbox Code Playgroud)

这不是 - 去图......

    [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");
        }
    }
Run Code Online (Sandbox Code Playgroud)