Code Review Stack Exchange is a question and answer site for peer programmer code reviews. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

share|improve this question
    
I would simplify the GetImage() method as suggested by @svick and I would also avoid the method call from the Razor view, build the list of files in a controller and then pass it to the view rather. – Uno Jul 2 at 17:26
up vote 5 down vote accepted

You could simplify your getImage() method pretty significantly to just:

public IEnumerable<string> GetImage() =>
    Directory.GetFiles(rootPath + @"\lib\Images\Nature").Select(Path.GetFileName);

Things I changed:

  1. Changed capitalization to follow .Net naming guidelines.
  2. Removed unnecessary method ReadPhotosFromDirectory().
  3. Removed unnecessary variable fileLocations.
  4. Used verbatim string to avoid having to escape backslashes.
  5. Used method group to delegate conversion instead of lambda.
  6. Got rid of ToArray(), which didn't serve any purpose, and changed return type to IEnumerable<string>.
  7. Used C# 6.0 expression bodied method.
share|improve this answer
    
Wow, that did not answer my question if it is a good way to display this way by passing in the image like that in the razor view, however you gave me a nice lesson to research. Yeah I am coming from Java background so naming is something I still will have to look around in .Net as well bunch of things I will look now differently at, big thanks :)) – vsarunov Jul 2 at 17:52
    
@vsarunov there is nothing wrong in using razor to display your images this way – Tolani Jaiye-Tikolo Jul 3 at 1:11
    
@TolaniJaiye-Tikolo Thank you :) – vsarunov Jul 3 at 11:25

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.