我正在尝试将OutputCache添加到具有WebImage.Write()响应的MVC Action,但是一旦我添加它(即使持续时间为0),内容类型也会从image / jpeg更改为text / html和我在浏览器中将图像呈现为文本。
示例代码 - 如果删除了OutputCache属性,这可以正常工作:
[OutputCache(Duration = 3000)]
public void GetImage(Guid id)
{
//Create WebImage from byte[] stored in DB
DbImage image = DbImageDAL.SelectSingle(e => e.DbImageId == id);
WebImage webimage = new WebImage(image.Data);
webImage.Write();
//Tried webImage.Write("JPEG"); but it makes not difference
}
答案 0 :(得分:5)
OutputCache会覆盖ContentType。您可以通过从OutputCacheAttribute派生类来解决此问题,如下所示:
public class ImageOutputCache : OutputCacheAttribute
{
public override void OnResultExecuting(ResultExecutingContext filterContext)
{
base.OnResultExecuting(filterContext);
filterContext.HttpContext.Response.ContentType = "image/jpeg";
}
}