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

发布于 2024-09-15 15:20:13 字数 914 浏览 6 评论 0原文

我正在构建一个使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

江湖彼岸 2024-09-22 15:20:13

更改操作

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);
}

并发送产品实例以查看并在“视图”中尝试此操作

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

Change 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);
}

and send a product instance to view and try this in View

<img src="<%: Url.Action("Show","Image",new { id = Model.Category.CategoryID  }) %>" />
柏拉图鍀咏恒 2024-09-22 15:20:13

尝试改用此方法:

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);
}

如果您不使用该扩展,这应该是基本方法。如果这有效,则错误在扩展中,如果这不起作用,则错误在其他地方 - 可能在路由中。另请检查古斯塔夫的回答!

Try to use this method instead:

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);
}

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!

甲如呢乙后呢 2024-09-22 15:20:13

我不确定这是否是您遇到的问题,但我总是将操作和控制器名称大写:

<%= Url.Action( "Show", "Image", new { id = product.Category.CategoryID } ) %> 

I'm not sure if that is the problem you have, but I always capitalize the action and controller names:

<%= Url.Action( "Show", "Image", new { id = product.Category.CategoryID } ) %> 
浮云落日 2024-09-22 15:20:13

结果我不得不使用匿名类型
' 这样路由就是 /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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文