0

I am trying to display some images on a page with this view

Details.cshtml

@foreach (var imagePath in Model.Images)
{
    <img src="@Url.Action("GetImage", "Receiving", new { @imagePath = imagePath })" />
}

ReceivingController.cs

public ActionResult GetImage(string imagePath)
{
    var folder = ConfigurationManager.AppSettings["ImagesPath"];
    var path = Path.Combine(folder, imagePath);
    byte[] image = System.IO.File.ReadAllBytes(path);
    return File(image, "image/jpg");
}

But when I set a breakpoint in the controller, it never gets hit and of course the images fail to load.

I can see the value being passed from the view is a path e.g. "C:\\inetpub\\wwwroot\\SiteName\\Images\\3000\\1\\1\\2.jpg"

I tried changing @Url.Action' to@Html.Action` just because I have no idea what else to try, but I got an error

OutputStream is not available when a custom TextWriter is used

I don't understand why the controller is not being called.

I noticed if I just navigate to the url Receiving\GetImage\whyareyounotworking, the controller action is fired.

I tried adding a route as below, but with no change

routes.MapRoute(
    name: "GetImage",
    url: "receiving/GetImage/{imagePath}",
    defaults: new { controller = "Receiving", action = "GetImage" }
);
Bassie
  • 9,529
  • 8
  • 68
  • 159
  • Why not just set the value of `imagePath` to the correct value - e.g. `"/Images/YourFileName.jpg"`? The issue is that the route contains a `.` (refer [this answer](https://stackoverflow.com/questions/11728846/dots-in-url-causes-404-with-asp-net-mvc-and-iis)) –  Dec 06 '17 at 06:29
  • @StephenMuecke I have been struggling with this for ages now. The image is sitting in a seperate website/app pool for a site which is only internal. However, this seperate, outward facing site needs to serve the images from the internal site. I am unsure how to build the relative url to do this or if its even possible. Seems it should be such an easy task just to display some images yet I just can't seem to do it.. – Bassie Dec 06 '17 at 06:33

2 Answers2

0

remove this:

@imagePath

So you will have an url.action like this:

<img src="@Url.Action("GetImage", "Receiving", new { imagePath = imagePath })" />
Barr J
  • 10,636
  • 1
  • 28
  • 46
0

It seems to be something to do with the string being passed in.

After removing the .jpg from the end of the file path before passing back to the controller my breakpoint now hits.

Bassie
  • 9,529
  • 8
  • 68
  • 159