【发布时间】: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