title: ASP.NET MVC Image Parameter Caching
body: <p>I have a controller action which retuns a ImageResult (extending ActionResult). When this action method is called the first time with parameter value N, other consequent calls will ALL have the same parameter value N, while their parameters differ. I guess this is somehow related to parameter caching in ASP.NET MVC. </p>
  
  <p>As result, return image by calling the action is always the same, regardless of the parameter value. Is there a way around this?</p>
  
  <p>Maybe its something related to directly writing to Response? Here's my ImageResult:</p>
  
  <pre>
  <code>
  public class ImageResult : ActionResult  
  {  
      public Image Image  
      {  
          get; set;  
      }  
  
      public ImageFormat ImageFormat  
      {  
          get; set;  
      }  
  
      private static Dictionary FormatMap  
      {  
          get; set;  
      }  
  
      static ImageResult()  
      {  
          CreateContentTypeMap();  
      }  
  
      public override void ExecuteResult(ControllerContext context)  
      {  
          if (Image == null) throw new ArgumentNullException("Image");  
          if (ImageFormat == null) throw new ArgumentNullException("ImageFormat");  
  
          context.HttpContext.Response.Clear();  
          context.HttpContext.Response.ContentType = FormatMap[ImageFormat];  
  
          Image.Save(context.HttpContext.Response.OutputStream, ImageFormat);  
      }  
  
      private static void CreateContentTypeMap()  
      {  
          FormatMap = new Dictionary  
          {  
              { ImageFormat.Bmp,  "image/bmp"                },  
              { ImageFormat.Gif,  "image/gif"                },  
              { ImageFormat.Icon, "image/vnd.microsoft.icon" },  
              { ImageFormat.Jpeg, "image/Jpeg"               },  
              { ImageFormat.Png,  "image/png"                },  
              { ImageFormat.Tiff, "image/tiff"               },  
              { ImageFormat.Wmf,  "image/wmf"                }  
          };  
      }  
  }  
  </code>
  </pre>
  
  <p>and the controller action:</p>
  
  <pre>
  <code>
  public ActionResult GetCalendarBadge(DateTime displayDate)
  {
      var bmp = SomeBitmap();
      var g = Graphics.FromImage(bmp);
  
      //GDI+ to draw the image.
  
      return new ImageResult { Image = bmp, ImageFormat = ImageFormat.Png };
  }
  </code>
  </pre>
  
  <p>and the view code:</p>
  
  <pre>
  <code>
  &lt;% foreach(var item in this.Model.News) { %&gt;  
        &lt;%= Html.Image&lt;NewsController&gt;(o => o.GetCalendarBadge(item.DisplayDate), 75, 75)%&gt;
  &lt;% } %&gt;
  </code>
  </pre>
  
  <p>Also tried adding these two avoid caching but nothing happened:</p>
  
  <pre>
  <code>
    context.HttpContext.Response.Cache.SetNoStore();  
    context.HttpContext.Response.Expires = 0;  
    context.HttpContext.Response.AppendHeader("Pragma", "no-cache");  
  </code>
  </pre>