asp.net core not loading static files

asp.net core wwwroot folder missing
asp.net core static files 404
asp.net core read file from wwwroot
asp.net mvc static files
asp.net core get wwwroot path
staticfileoptions
asp.net core static file authorization
usewebroot

I have an app .NET core 2.1 with this code:

 public void Configure(IApplicationBuilder app, IHostingEnvironment env)
    {
        app.UseStaticFiles();
        app.UseStaticFiles(new StaticFileOptions
        {
            FileProvider = new PhysicalFileProvider(
        Path.Combine(Directory.GetCurrentDirectory(), "Assets")),
            RequestPath = "/Assets"
        });
        if (env.IsDevelopment())
        {
            app.UseDeveloperExceptionPage();
        }
        else
        {
            app.UseHsts();
        }

        app.UseHttpsRedirection();
        app.UseMvc();
    }

and my structure folder:

but none of these urls open the image:

"my-website.com/images/snes/alien.jpg"

"my-website.com/wwwroot/images/snes/alien.jpg"

"my-website.com/Assets/Snes/alien.jpg"

anybody know what is wrong?

Edit: Here is the folder get by CurrentDirectoy() method (apparently is correct):

Edit2: With this code work on localhost but not when i publish on azure:

 app.UseFileServer(
        new FileServerOptions()
        {
            FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), "wwwroot"))
        });

You are most likely in a working directory that is different than the one you think. Please check this by setting a breakpoint on foo:

public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    var foo = Directory.GetCurrentDirectory();
}

The solution depends in how you start the application.

If you are doing it via Visual Studio, probably you have set another Working Directory in the Project properties?

If via command line, you need to cd to your project root.

Another solution, would be to use the directory of your assembly:

// get the directory
var assemblyDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location));
var assetDirectory = Path.Combine(assemblyDirectory, "Assets"));

// use it
app.UseStaticFiles(new StaticFileOptions
{
    FileProvider = new PhysicalFileProvider(assetDirectory),
    RequestPath = "/Assets"
});

Static files in ASP.NET Core, If the IIS static file handler is enabled and the ASP.NET Core Module (ANCM) is not correctly configured (for example if web.config was not deployed), static files  Re: ASP.net core does not load static files in internet explorer but works fine in Chrome. use static files assumes there is a wwwroot folder in the working folder, that has the static files. you can configure the static file location, see the docs.

use IHostingEnvironment

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

Working with Static Files - ASP.NET Core Documentation, ASP.NET Core application cannot serve static files by default. We must include Microsoft.AspNetCore.StaticFiles middleware in the request pipeline. Install  Stack Overflow for Teams is a private, secure spot for you and your coworkers to find and share information. Learn more asp.net core not loading static files

If you are using visual code then you have to set up your working directory in cwd parameter of configuration array of your lunch.json file. See attached screenshot.

ASP.NET Core - Serving Static Files, Hi Folks, I have hosted an ASP.NET Core application in IIS by following this article and I face some challenges while browsing the application  I am getting Failed to load resource: the server responded with a status of 404 (Not Found) for my static resources under wwwroot folder after the deployment of ASP.NET Core 2.2 mvc web application on Ubuntu 18.04/Nginx in digitalocean.

Static assets are not loading while hosting an ASP.NET Core , hi guys I just deployed a new asp.net core 3.0 project, but it seems most of the files are not loading, e.g. the boostrap, css files and lots of  When running a Asp.Net Core 2.0 (or 2.2) app using IIS express, static files (css, js) are served as expected. However when using command line/Kestrel via "dotnet publish -o [targetDirectory]" and dotnet [website.dll], Kestrel does not serve up any static content. Utilizing browser F12, I see Kestrel returns a 404 error.

Asp.net core 3.0 - static files dont load, hi guys I just deployed a new asp.net core 3.0 project, but it seems most of the files are not loading, e.g. the boostrap, css files and lots of  Here, we will learn how to serve static files such as html, JavaScript, CSS, or image files on HTTP request without any server-side processing. ASP.NET Core application cannot serve static files by default. We must include Microsoft.AspNetCore.StaticFilesmiddleware in the request pipeline.

ASP.NET Core 3.0 does not load static content and all images , Describe the bug Static files included in project but in FOLDER GitHub is home to over 50 million developers working together to host and review code, manage projects, and build software together. Sign up Could not extract contents of the bundle https://github.com/aspnet/AspNetCore/issues/12020. In ASP.NET core, by default, static files are served from only the wwwroot folder. That means if you try to access a file from Libs directory, it won't work. The good news is, You can configure the static file location as you wish. So update your Configure method in startup.cs.

Comments
  • what is your current working directory?
  • Can you try this? stackoverflow.com/a/47385434/495455
  • How did you publish to Azure? Fail to reproduce your issue, this "my-website.com/Assets/Snes/alien.jpg" should be able to open this image. Did you get 404 error or anything else?
  • apparently the working directory is correct. It is one level above the "wwwroot" folder. I edited the post, look the image.
  • I am using ASP.NET Core 3.1 and this worked best for me.
  • That solved my problem, thanks