MVC WebImage输出缓存结果的内容类型为text/html

本文关键字:类型 text html 输出 WebImage 缓存 结果 MVC | 更新日期: 2023-09-27 18:18:28

我试图添加一个OutputCache到一个MVC动作,有一个WebImage.Write()响应,但只要我添加它(即使持续时间为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
}

MVC WebImage输出缓存结果的内容类型为text/html

OutputCache覆盖ContentType。您可以通过从OutputCacheAttribute中派生一个类来修复这个问题,如下所示:

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