如何在 asp.net mvc 中渲染图像?
我正在构建一个使用 Northwind 数据库的示例。 我有一个视图,其中显示特定类别的所有产品,并使用 ul 生成带有图像以及产品名称和价格的项目。
我在这里使用了代码, http://blogs.msdn.com/b/miah/archive/2008/11/13/extending-mvc-returning-an-image-from-a-controller-action .aspx 。
并且已经达到这样的程度:如果我右键单击页面上的图像,我会得到 以下为图片网址。
这是我提供的操作方法,它只接受类别 ID。 /image/show/1
我的 ImageController 中的操作方法如下:
//
// 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[]
然后我在我的视图中这样调用它。 (产品是我认为的模型)
但我仍然无法显示图像。
I have a sample I'm building, that's using the Northwind database.
I have a view in which I show all the products for a specifc category and use a ul to generate items with an image and the products name and price.
I have used the code here, http://blogs.msdn.com/b/miah/archive/2008/11/13/extending-mvc-returning-an-image-from-a-controller-action.aspx .
And have gotten to the point that if I right-click an image on my page I get
the follow for the image url.
This is the action method I provided, which just takes the Categores ID.
/image/show/1
My action method in my ImageController is as follows:
//
// 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);
}
Note: Picture is a byte[]
I then call it in my view like this. (product is the Model for my view)
But I still can't get the image to be displayed.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
更改操作
并发送产品实例以查看并在“视图”中尝试此操作
Change action
and send a product instance to view and try this in View
尝试改用此方法:
如果您不使用该扩展,这应该是基本方法。如果这有效,则错误在扩展中,如果这不起作用,则错误在其他地方 - 可能在路由中。另请检查古斯塔夫的回答!
Try to use this method instead:
This should be basic approach if you don't use that extension. If this works the error is in extension if this doesn't work the error is somewhere else - probably in routing. Also check Gustav's answer!
我不确定这是否是您遇到的问题,但我总是将操作和控制器名称大写:
I'm not sure if that is the problem you have, but I always capitalize the action and controller names:
结果我不得不使用匿名类型
' 这样路由就是 /Image/Show/1,而不是 /Image/Show?CategoryID=1。当然,还需要将 Northwind 中的图像从位图更新为 Jpeg。
Turns out I had to use an anoynomus type
' so that the route was /Image/Show/1, instead of /Image/Show?CategoryID=1. That and of course needed to update the images in Northwind from bitmap to Jpeg.