How to make an image in asp.net mvc?

I have a sample that I am building using the Northwind database. I have a view in which I show all products for the specificc category and use ul to create elements with the image and the name and price of the product.

I 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 came to the conclusion that if I right-click the image on my page, I will get follow the URL of the image.

This is the action method I provided that just accepts the category identifier. / Photo / 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. Image is a byte []

Then I will call it in my opinion as follows. (product is a model for my presentation)

But I still cannot get the image to be displayed.

+4
source share
4 answers

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 copy of the product for viewing and try it in the view

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

Try using 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 a basic approach if you are not using this extension. If it works, an error occurs in the extension, if it does not work, the error is located somewhere else - possibly in routing. Also check out Gustav's answer!

+1
source

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

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

Turns out I had to use the anoeic type 'so that the route was / Image / Show / 1, instead of / Image / Show? CategoryID = 1. This and, of course, it was necessary to update the images in Northwind from bitmap to Jpeg.

0
source

All Articles