title: Caching ASHX Image Response
body: <p>I have created an ashx file to generate image thumbnails on the fly. I would like to cache these images client side after they are called the first time.</p>
  
  <p>URL:</p>
  
  <blockquote>
    <p>~/image.ashx?dir=user&amp;w=25&amp;h=25&amp;force=yes&amp;img=matt.jpg</p>
  </blockquote>
  
  <p>Code Behind:</p>
  
  <pre><code>public void ProcessRequest (HttpContext context) {
      TimeSpan refresh = new TimeSpan(0, 15, 0);
      context.Response.Cache.SetExpires(DateTime.Now.Add(refresh));
      context.Response.Cache.SetMaxAge(refresh);
      context.Response.Cache.SetCacheability(HttpCacheability.Server);
      context.Response.CacheControl = HttpCacheability.Public.ToString();
      context.Response.Cache.SetValidUntilExpires(true);
  
      string dir = context.Request.QueryString["dir"];
      string img = context.Request.QueryString["img"];
      bool force = context.Request.QueryString["force"] == "yes";
      double w = 0;
      double h = 0;
      ...
      context.Response.ContentType = "image/jpeg";
      thumb.Save(context.Response.OutputStream, System.Drawing.Imaging.ImageFormat.Jpeg);
  }
  </code></pre>
  
  <p>Using the Dev Tools in Chrome I can see the following Response Header:</p>
  
  <pre><code>HTTP/1.1 200 OK
  Server: ASP.NET Development Server/10.0.0.0
  Date: Mon, 26 Sep 2011 19:17:31 GMT
  X-AspNet-Version: 4.0.30319
  Set-Cookie: .ASPXAUTH=...; path=/; HttpOnly
  Cache-Control: no-cache
  Pragma: no-cache
  Expires: -1
  Content-Type: image/jpeg
  Content-Length: 902
  Connection: Close
  </code></pre>
  
  <p>Can someone enlighten me as to why this isn't caching on multiple calls with the same exact URL? Any tips would be greatly appreciated.</p>