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

Rob*_*Rob 2 c# outputcache asp.net-mvc-4 webimage

我正在尝试将一个OutputCache添加到具有WebImage.Write()响应的MVC Action,但是一旦我添加它(即使持续时间为0),内容类型也会从image/jpeg更改为text/html和I获取在浏览器中以文本形式呈现的图像.

示例代码 - 如果删除了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
}
Run Code Online (Sandbox Code Playgroud)

Pet*_*ete 5

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

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