【问题标题】:ASP.NET MVC Image Parameter CachingASP.NET MVC 图像参数缓存
【发布时间】:2009-04-25 10:25:17
【问题描述】:

我有一个控制器操作,它可以重新调整 ImageResult(扩展 ActionResult)。当这个动作方法第一次被参数值 N 调用时,其他后续调用都将具有相同的参数值 N,而它们的参数不同。我猜这在某种程度上与 ASP.NET MVC 中的参数缓存有关。

因此,无论参数值如何,调用动作返回的图像总是相同的。有没有办法解决这个问题?

也许它与直接写入响应有关?这是我的 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" } }; } }

和控制器动作:

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 }; }

以及查看代码:

<% foreach(var item in this.Model.News) { %> <%= Html.Image<NewsController>(o => o.GetCalendarBadge(item.DisplayDate), 75, 75)%> <% } %>

还尝试添加这两个避免缓存但没有任何反应:

context.HttpContext.Response.Cache.SetNoStore(); context.HttpContext.Response.Expires = 0; context.HttpContext.Response.AppendHeader("Pragma", "no-cache");

【问题讨论】:

    标签: asp.net-mvc


    【解决方案1】:

    我认为该行为是通过操作上的缓存属性“选择加入”的。

    您确定您的数据访问层正常工作吗?

    【讨论】:

    • 是的,ViewModel 包含正确的数据。如果我将值直接打印到页面上,它会被正确打印,但控制器动作总是用第一个(以前调用的)值调用。
    【解决方案2】:

    发现问题了!这是由于使用 Windsor IoC 并将控制器注册/解析为 Singleton(这是默认行为)。将生活方式更改为 Transient 解决了该问题(以及其他问题)。

    请参阅here 了解更多信息。

    【讨论】:

    • IOW,这个问题与 ASP、缓存或图像无关。 =]
    猜你喜欢
    • 1970-01-01
    • 2014-03-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-18
    • 2011-03-27
    • 2012-02-23
    • 1970-01-01
    相关资源
    最近更新 更多