image not displaying in visual studio .net 8 cshtml

iqworks Information Quality Works 296 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

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,764 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,582 questions
Visual Studio
Visual Studio
A family of Microsoft suites of integrated development tools for building applications for Windows, the web and mobile devices.
5,391 questions
0 comments No comments
{count} votes

Accepted answer
  1. Ruikai Feng - MSFT 2,601 Reputation points Microsoft Vendor
    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) 70,376 Reputation points
    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.