Image is getting stored in root folder but while fetching image not displayed on page asp core mvc

NIKITA HANDE 0 Reputation points
2023-11-02T12:22:02.72+00:00

Image is getting stored in the root folder but while fetching it is not displayed on page when I inspected img src was also correct and on console 404 error come please can anyone help bcoz I am doing this from long time

ASP.NET Core
ASP.NET Core
A set of technologies in the .NET Framework for building web applications and XML web services.
4,502 questions
ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,451 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,031 questions
{count} votes

1 answer

Sort by: Most helpful
  1. JasonPan - MSFT 5,451 Reputation points Microsoft Vendor
    2023-11-03T03:15:59.9966667+00:00

    Hi @NIKITA HANDE,

    We highly recommend putting static resources in the wwwroot folder. It is not recommended to put it anywhere else, as this will protect the security of your static resources.

    The middleware UseStaticFiles(default settings) only supports reading the contents of the wwwroot folder, so this is the reason why you encounter 404 when reading resources in the root directory.

    If you want to read the resources in the root directory, you can do so in the following ways .
    Please note that this is not the recommended approach.

    1.My Project Sample
    User's image

    2.We need to add follow code in Program.cs

    app.UseHttpsRedirection();
    app.UseStaticFiles();
    
    app.UseStaticFiles(new StaticFileOptions
    {
        FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory())), 
        RequestPath = "" 
    });
    app.UseRouting();
    
    app.UseAuthorization();
    

    3.Test Result
    User's image


    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,

    Jason

    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.