2011-01-07 8 views
12

Sto cercando di aggiungere un'intestazione "max-age" alla mia risposta. Funziona bene sul mio Visual Studio Development Server, ma non appena sposto l'applicazione su IIS (ho provato sia IIS Express localmente che IIS sul server) - l'intestazione scompare.Cache.SetMaxAge non funziona in IIS, funziona bene con VS Dev Srv

Il mio codice:

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

risposta del server VS Dev (tutto funziona bene):

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 risposta

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. Si tratta di un ASHX-gestore, se è importante ...

risposta

25

UPDATE: 2011-03-14 La correzione è garantire si chiama SetSlidingExpiration (vero)

context.Response.Cache.SetCacheability(HttpCacheability.Public); 
context.Response.Cache.SetMaxAge(TimeSpan.FromMinutes(5)); 
context.Response.ContentType = "image/jpeg"; 
context.Response.Cache.SetSlidingExpiration(true); 

Se si rimuove il modulo OutputCache si vuole ottieni il risultato desiderato Lo vedo come un insetto.

Così, nel web.config si dovrebbe effettuare le seguenti operazioni:

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

aggiunto: so, ci sono ulteriori informazioni.

  1. Uso OutputCacheAttribute di MVC apparentemente non ha questo problema
  2. Sotto la stessa applicazione MVC, senza rimuovere "OutputCache" dai moduli, un'implementazione diretta se IHttpHandler o un ActionResult risultati nel s-MaxAge essendo spogliato

il seguente spoglia il 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); 
     }    
    } 

il seguente spoglia il 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"); 
     } 
    } 

Questo non significa - vai a capire ...

[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

Soluzione:

in web.config:

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

e en IIS PC:

Con cmd go a c:\windows\system32\inetsrv.

poi eseguire:

appcmd unlock config /section:staticContent 
0

tardiva risposta, ma questo potrebbe aiutare qualcuno: -

Response.Cache.SetProxyMaxAge(TimeSpan.Zero); 
Problemi correlati