Problem displaying image in ASP.NET MVC Application

ChrisU 231 Reputation points
2023-02-17T15:39:23.43+00:00

I am having some trouble displaying an image in an ASP.NET MVC application.  See below screen grab which shows the code I am using and the folder structure.

User's image

So I am using <img src="~/Content/Images/logo.png" /> to display the logo.png found in the Content/Images directory.

It does not display and I get the broken image icon.

I have app.UseStaticFiles(); in my Program.cs file

If anyone can help it would be appreciated.

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,449 questions
{count} votes

Accepted answer
  1. Viorel 116.5K Reputation points
    2023-02-17T16:02:16.0933333+00:00

    Try creating the images subfolder of wwwroot and move here the logo.png file.

    Then use <img src="~/images/logo.png"/>.

    3 people found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Barney 10 Reputation points
    2023-06-26T08:45:15.94+00:00

    You have to specific your static file location.

                app.UseStaticFiles(new StaticFileOptions(){
    			FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"Content/Images")), RequestPath = new PathString("/Content/Images")});
    
    2 people found this answer helpful.
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.