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