gpt4 book ai didi

c# - 如何在 asp.net mvc 中渲染图像?

转载 作者:太空狗 更新时间:2023-10-29 22:20:45 24 4
gpt4 key购买 nike

我有一个正在构建的示例,它使用 Northwind 数据库。我有一个 View ,其中显示特定类别的所有产品,并使用 ul 生成带有图像以及产品名称和价格的项目。

我在这里使用了代码,http://blogs.msdn.com/b/miah/archive/2008/11/13/extending-mvc-returning-an-image-from-a-controller-action.aspx .

并且已经到了这样的地步,如果我右键单击我页面上的图像,我会得到图像 url 的后续内容。

这是我提供的操作方法,它仅采用类别 ID。/图像/显示/1

我的 ImageController 中的 action 方法如下:

    //
// GET: /Image/Show
public ActionResult Show(int id)
{
var category = northwind.AllCategories().Single(c => c.CategoryID == id);
byte[] imageByte = category.Picture;
string contentType = "image/jpeg";

return this.Image(imageByte, contentType);
}

注:图片是一个byte[]

然后我在我看来是这样调用它的。 (产品是我的模型)

但是还是无法显示图片。

最佳答案

改变 Action

public FileContentResult Show(int id)
{
var category = northwind.AllCategories().Single(c => c.CategoryID == id);
byte[] imageByte = category.Picture;
string contentType = "image/jpeg";

return File(imageByte, contentType);
}

并发送一个产品实例以在 View 中查看和尝试

<img src="<%: Url.Action("Show","Image",new { id = Model.Category.CategoryID  }) %>" />

关于c# - 如何在 asp.net mvc 中渲染图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3540964/

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