I am trying to display all the images in a images folder I have stored in the root file. I know they have vaulted the Server.MapPath method in asp.net core. I am not sure how I would do the same functionality in .net core of creating a view model and being able to loop through all the images stored in my root images folder. Any suggestions would be great. Below is the example code I was going off of but it obviously doesn't work for.Net core.
// model
class MyViewModel
{
public IEnumerable<string> Images { get; set; }
}
// controller
public ActionResult MyAction()
{
var model = new MyViewModel()
{
Images = Directory.EnumerateFiles(Server.MapPath("~/images_upload"))
.Select(fn => "~/images_upload/" +
Path.GetFileName(fn))
};
return View(model);
}
// view
@foreach(var image in Model.Images)
{
<img src="@Url.Content(image)" alt="Hejsan" />
}