Skip to main content

ASP.NET Core Web API and 404 error

How to resolve if your ASP.NET Core Web API is throwing 404 errors on static content?

ASP.NET / .NET 8 / .NET 9 Freehosting
If you don't already have our ASP.NET / .NET Core Freehosting, sign up for FREE at https://MonsterASP.net/.

Description

You are using ASP.NET Core Web API application and you need to link static files throught this API. In most cases you need link static files like images. By default ASP.NET Core Web API does not allow any requests to static content and you will always receive 404 error. In that case you'll need to adjust your application source code like this:

Update your Configure code like this:

app.UseStaticFiles();
app.UseStaticFiles(new StaticFileOptions()
{
  FileProvider = new PhysicalFileProvider(Path.Combine(Directory.GetCurrentDirectory(), @"Files")),
  RequestPath = new PathString("/Files")
});

This code will cause that all requests pointing to /Files folder pass through your Web API and return linked static file for download or display it.

More informations can be found there:

https://stackoverflow.com/questions/71853420/images-not-being-served-in-asp-net-core-web-api