Displaying Image within Project folder

Dean Everhart 1,536 Reputation points
2023-03-24T13:48:07.83+00:00

Any idea why an image wouldn't display in core 6? Is there a problem with the line of code below...the way the path is transcribed? Is there a problem using png format in 6?

<img src="/Media/Home.png" alt="Home Page" width="500" height="600">

User's image

User's image

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,559 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,489 questions
0 comments No comments
{count} votes

Accepted answer
  1. AgaveJoe 28,371 Reputation points
    2023-03-24T13:58:43.21+00:00

    In ASP.NET Core, static files are located in the wwwroot folder. Please read the official static file handler documentation.

    Static files in ASP.NET Core

    If you want to return a file that is not located in wwwroot then a little code is required as illustrated in your other thread within the same subject.

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Farid Uddin Kiron MSFT 456 Reputation points Microsoft Vendor
    2023-03-27T02:51:33.3833333+00:00

    As you may know, Static files are stored within the project's web root directory and by default and the directory is {content root}/wwwroot, but in order to serve your static files outside of wwwroot directory you would require explicitly define static file configuration within program.cs file.

    Let's implement as per your scenario. According to your project hierarchy, you have following static folder called Media:

    FolderReference

    Static file configuration in program.cs:

    app.UseStaticFiles(new StaticFileOptions
    {
        FileProvider = new PhysicalFileProvider(
               Path.Combine(builder.Environment.ContentRootPath, "Media")),
        RequestPath = "/Media"
    });
    

    HTML:

    <img src="/Media/Home.png" alt="Home Page" width="500" height="600">

    Output:

    StaticImage

    Note: If you would like to know more details on how to Serve files outside of web root you could check our official document here

    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.