I have a question concerning the correctness of the reading and displaying the image in ASP.NET Core MVC web application.
This is the way I am reading image names from a specific folder (I am using IHostingEnvironment _hostingEnvironment
to get the rootPath
):
public class GetRandomImageForGalleryView : IGetRandomImageFromFolder
{
private string rootPath;
public GetRandomImageForGalleryView(string rootPath)
{
this.rootPath = rootPath;
}
public string[] getImage()
{
return ReadPhotosFromDirectory();
}
private string[] ReadPhotosFromDirectory()
{
string[] fileLocations = Directory.GetFiles(rootPath+"\\lib\\Images\\Nature").Select(path => Path.GetFileName(path))
.ToArray();
return fileLocations;
}
}
And this is the way I am displaying them:
@model IGetRandomImageFromFolder
@{
ViewBag.Title = "Gallery";
}
<div class="container">
<div class="row">
@{
foreach (var item in Model.getImage())
{
<img src="~/lib/Images/Nature/@item" style="max-width: 500px;">
}
}
</div>
</div>
Basically I just replace the image name.
Is this a good way? If not, why? Any other remarks would be helpful as well.