Asp.Net Core 2.0-2.2 Kestrel not serving static content

I am unable to reproduce your error using a fresh ASP.NET Core 2.0 project following these steps;

  1. md static-test
  2. cd static-test
  3. dotnet new web
  4. Add folder css in wwwroot.
  5. Add file site.css in wwwroot/css.
  6. Insert app.UseStaticFiles(); at the start of Startup.Configure() method.
  7. dotnet publish -o pubweb
  8. cd pubweb
  9. dotnet .\static-test.dll
  10. Access http://localhost:5000/css/site.css using browser.

dotnet.exe renders the following output in my terminal:

Hosting environment: Production
Content root path: C:\src\static-test\pubweb
Now listening on: http://localhost:5000
Application started. Press Ctrl+C to shut down.
info: Microsoft.AspNetCore.Hosting.Internal.WebHost[1]
      Request starting HTTP/1.1 GET http://localhost:5000/css/site.css
info: Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware[2]
      Sending file. Request path: '/css/site.css'. Physical path: 'C:\src\static-test\pubweb\wwwroot\css\site.css'

As you can see it will successfully serve the css file within a subfolder correctly. Please try the above steps and compare the code and output with your failing project. If it is still failing, please attach the debug info on Request vs. Physical path from Microsoft.AspNetCore.StaticFiles.StaticFileMiddleware above.


I tried so many useless things, this was the fix for me:

WebHost.CreateDefaultBuilder(args)
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.UseWebRoot("E:\\xyz\\wwwroot")
.UseUrls("http://localhost:5050")
    .Build();

Serving static files started working as soon as I added:

.UseWebRoot("E:\\xyz\\wwwroot")

I got a conflict on one of my services running on port 5000, so I specified to start on port 5050.


Update for .Net Core 2.2

For .NET Core 2.2, the following works: .UseWebRoot("wwwroot")

However, a more readable and explicit approach to get the path:

static string webRoot = Path.Combine(AppContext.BaseDirectory, "wwwroot");

and then UseWebRoot(webRoot);


For me, the problem was the working directory. I wasn't paying attention to the directory I was in when trying to launch the app with dotnet /var/www/project/project.dll. It automatically uses your current directory as the working directory when you launch the app this way.

I realized this when I looked at a .service file for another project, which has the WorkingDirectory specified:

...
WorkingDirectory=/var/www/project/
ExecStart=/usr/bin/dotnet /var/www/project/project.dll
...

So, either make sure you are in the correct directory when you run your project, or ensure that the WorkingDirectory is properly set in your .service file.