简体   繁体   English

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

[英]How can I render an image in asp.net mvc?

I have a sample I'm building, that's using the Northwind database. 我有一个我正在构建的示例,它正在使用Northwind数据库。 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. 我有一个视图,其中我显示了特定类别的所有产品,并使用ul生成带有图像的项目以及产品名称和价格。

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 . 我在这里使用了代码, 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. 并且已经达到了这一点,如果我右键单击页面上的图像,我会得到图像URL的跟随。

This is the action method I provided, which just takes the Categores ID. 这是我提供的操作方法,它只接受分类ID。 /image/show/1 /图像/显示/ 1

My action method in my ImageController is as follows: 我的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);
    }

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. 但我仍然无法显示图像。

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 并发送产品实例以查看并在View中尝试此操作

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

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! 还检查古斯塔夫的答案!

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

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

Turns out I had to use an anoynomus type ' so that the route was /Image/Show/1, instead of /Image/Show?CategoryID=1. 事实证明我必须使用anoynomus类型'以便路由是/ Image / Show / 1,而不是/ Image / Show?CategoryID = 1。 That and of course needed to update the images in Northwind from bitmap to Jpeg. 那当然需要将Northwind中的图像从位图更新为Jpeg。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM