2011-01-07 70 views
12

我正在嘗試爲我的響應添加一個「max-age」標題。它在我的Visual Studio開發服務器上工作正常,但是當我將應用程序移動到IIS時(嘗試在本地表示IIS和在服務器上使用IIS) - 標題消失。Cache.SetMaxAge不能在IIS下工作,在VS Dev Srv下工作正常

我的代碼:

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處理程序,如果它很重要...

回答

25

更新:2011-03-14解決方法是確保你打電話SetSlidingExpiration(真)

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或S-最大生存週期一個ActionResult結果

以下條的s最大生存週期

  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最大生存週期

  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"); 
     } 
    } 
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 
0

遲來回答,但是這可能幫助別人: -

Response.Cache.SetProxyMaxAge(TimeSpan.Zero);