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.
As result, return image by calling the action is always the same, regardless of the parameter value. Is there a way around this?
Maybe its something related to directly writing to Response? Here's my ImageResult:
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" }
};
}
}
and the controller action:
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 };
}
and the view code:
<% foreach(var item in this.Model.News) { %>
<%= Html.Image<NewsController>(o => o.GetCalendarBadge(item.DisplayDate), 75, 75)%>
<% } %>
Also tried adding these two avoid caching but nothing happened:
context.HttpContext.Response.Cache.SetNoStore();
context.HttpContext.Response.Expires = 0;
context.HttpContext.Response.AppendHeader("Pragma", "no-cache");