c# - Cache.SetMaxAge 在 IIS 下不工作,在 VS Dev Srv 下工作正常

标签 c# asp.net iis browser-cache

我试图在我的回复中添加一个“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

附言。它是一个 ASHX 处理程序,如果重要的话......

最佳答案

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

关于c# - Cache.SetMaxAge 在 IIS 下不工作,在 VS Dev Srv 下工作正常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4626780/

相关文章:

c# - 在 Global.asax 的 Application_Start 方法中访问缓存时出错

c# - 如何以多种语言发送核心银行系统生成的警报?

c# - Visual Studio 2015 Intellisense 无法确定某些泛型方法中的 lambda 类型

c# - 获取具有给定列标题的列的索引 C#

c# - 如何在linq和EF中做一个简单的搜索功能

jquery - 为什么 Ajax 脚本无法在 IIS 7.5 Win 2008 R2 服务器上运行?

asp.net-mvc - ASP.NET MVC : CSS file returning a 302 error when it exists

c# - 如何处理范围性质的服务,如 DI 和 IOC 环境中的事务

c# - 为什么 Java on Server 和 C# on Client 是流行的选择?

c# - 表达式树