gpt4 book ai didi

c# - MVC WebImage OutputCache 导致内容类型为 text/html

转载 作者:行者123 更新时间:2023-12-04 02:11:22 24 4
gpt4 key购买 nike

我正在尝试将 OutputCache 添加到具有 WebImage.Write() 响应的 MVC 操作,但是一旦我添加它(即使持续时间为 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
}

最佳答案

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

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

关于c# - MVC WebImage OutputCache 导致内容类型为 text/html,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17088972/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com