ASP.NET - 404 file not found on a image, CSS, Javscript or another static file? (updated 2020)

If all your files in your wwwroot (or whatever you call your web root) folder gives you a 404. Then you have most likely misconfigured your application. In 2020 I have updated this post with a new section: "newer than Asp.Net core 2". Here I describe how to fix this in newer versions of Asp.net core. The original post was on how to do this in Asp.net core 2 and previous versions.

Newer than Asp.net core 2

First you need to check that you are making a call to UseStaticFiles();. This should be in your startup.cs file. It will look like the following:

app.UseStaticFiles();

Without this you will not be able to serve any "static" files such as Javascript, CSS or images. In most situations the folder that contains your static files is called "wwwroot". If this is not the case that might be the issue you are facing. Per default it is named "wwwroot", but this can be changed. If you wish to use another folder name, use the following syntax for "app.UseStaticFiles()":

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

In the above I have chosen to use the folder "SomeOtherwwwroot" by providing this to the StaticFileOptions. Using RequestPath you can also change the root URL for your static files, providing "" places the static files at root level.

This is all you need to make static files available on your site. Please let me know in the comments if this helped you or if you are still struggling with this.

Asp.net Core 2 (original post)

First of all check that you are making a call to UseStaticFiles();. This has to be in your startup.cs file that you use to configure your webhost. The call can look like below. If you do not already have a reference to Microsoft.AspNetCore.StaticFiles, then you will need that.

app.UseStaticFiles();

A less likely reason why you get a 404. is that the static files are not placed within the webroot. It could be that the webroot is set to a different location that you first anticipated. Your webroot is configured together with your WebHostBuilder by using .UseWebRoot("YourPath");. For example:

var host = new WebHostBuilder()
   .UseKestrel()
   .UseWebRoot("YourPath")

I hope this helps, did I miss something? Please let me know in the comments.