I am super new to ASP.NET and I'm having trouble displaying a single simple image to the web app.
I have uploaded my image file to wwwroot/images
and also have set the model and the controller. I also tried simple img src and the image path to it but still the image is not recognized.
The image is not error whatsoever but the web app is not displaying it...
Here is my model:
using System;
namespace FirstApp.Models
{
public class ImageModel
{
public int Id { get; set; }
public string Name { get; set; }
public string ImagePath { get; set; }
}
}
Here is my controller
public IActionResult DisplayImage(int Id)
{
var image = new ImageModel
{
Id = Id,
Name = "Image",
ImagePath = "images/img.jpeg"
};
return View(image);
}
And here is my .cshtml
file
@model FirstApp.Models.ImageModel
@{
ViewData["Title"] = "Home Page";
}
<div class="text-center">
<h1 class="display-4">@Model?.Name</h1>
<img src="/@Model?.ImagePath" alt="My Image hehehe"/>
</div>
Here is my web app
Any kind of help is appreciated. Thank you.
I test your code, and I can display single image saved in wwwroot/images, try to check the name of image is same in the ImagePath like:
ImagePath = "images/flower.PNG"
structure:
result: