image not displaying in visual studio .net 8 cshtml

iqworks Information Quality Works 331 Reputation points
2025-01-30T01:06:43.91+00:00

I created a simple .net 8.0 web application. I put what seemed to be a simple <src> statement, but the image does not show up on the browser.

  The image is in a folder called “images”

 vs image2

   The image is coded here. when i hover over the image it displays which means this statement should work.

vs image3

when i run the page, it shows this.vs image5

  Thanks for any suggestions or advice

Developer technologies | ASP.NET | ASP.NET Core
Developer technologies | .NET | Other
Developer technologies | Visual Studio | Other
Developer technologies | ASP.NET | Other
0 comments No comments
{count} votes

Accepted answer
  1. Anonymous
    2025-01-30T08:51:36.68+00:00

    Two solution for you:

    1,call staticfile middleware as below in Program.cs:

    app.UseStaticFiles();    //Serve files from wwwroot
    app.UseStaticFiles(new StaticFileOptions
    {
        FileProvider = new PhysicalFileProvider(
                Path.Combine(builder.Environment.ContentRootPath, "images")),
        RequestPath = "/images"
    });//Serve files from images
    
    
    

    Result:

    User's image

    2,move your images folder into wwwroot

    You could read this document to learn more about asp.net core staticfiles


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    Best regards,

    Ruikai Feng

    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Bruce (SqlWork.com) 77,686 Reputation points Volunteer Moderator
    2025-01-30T17:41:56.72+00:00

    The images should be under the wwwroot folder, not at the project root.

    1 person 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.