MVC WebImage OutputCache导致text / html的内容类型

时间:2013-06-13 13:58:57

标签: c# asp.net-mvc-4 outputcache webimage

我正在尝试将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
}

1 个答案:

答案 0 :(得分:5)

OutputCache会覆盖ContentType。您可以通过从OutputCacheAttribute派生类来解决此问题,如下所示:

public class ImageOutputCache : OutputCacheAttribute
{
    public override void OnResultExecuting(ResultExecutingContext filterContext)
    {
        base.OnResultExecuting(filterContext);
        filterContext.HttpContext.Response.ContentType = "image/jpeg";
    }
}