tags:

views:

548

answers:

2

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

A: 

I thought that behaviour was 'opt-in' via the caching attributes on the action.

Are you absolutely sure your data access layer is working as it should?

IainMH
Yes, the ViewModel contains correct data. If I print the value directly to the page, it is printed correctly, but controller action is always called with the first (previously called) value.
Hadi Eskandari
A: 

Found the problem! It is due to using Windsor IoC and registering / resolving the controllers as Singleton (which is the default behavior). Changing the lifestyle to Transient fixed the issue (along with other issues).

See here for more info.

Hadi Eskandari
IOW, this question has nothing to do with ASP, caching or images. =]
bzlm