Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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. Looking closer, when entering the file path directly in the browser (localhost:5000/css/cssfile.css) the file is not displayed but the browser appears to redirect to "localhost:5000/cssfile.css" but still returns a 404 error (note the missing /css/ directory).

I created this project via Visual Studio 2017, and chose the defaults for a new MVC Core 2.0 application (SDK was installed).

I have followed the steps here to enable static files in the program.cs and startup.cs files. These implement "app.UseStaticFiles();" and ".UseContentRoot(Directory.GetCurrentDirectory())". None of the articles found via google seem to help. I have verified dotnet copied the static content to the target directory.

What am I missing? Thanks

// Program.cs
public static IWebHost BuildWebHost(string[] args) => WebHost
   .CreateDefaultBuilder(args)
   .UseStartup<Startup>()
   .Build();

// Startup.cs
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
    app.UseExceptionHandler("/Error/HandleError");
    app.UseStaticFiles();

    app.UseMvc(routes =>
    {
        routes.MapRoute( name: "default", template: "{controller=User}/{action=Index}/{id?}");
    });
}
like image 900
davewilliams459 Avatar asked Sep 11 '17 17:09

davewilliams459


People also ask

What is the default directory for static files in ASP.NET Core?

Static files are stored within the project's web root directory. The default directory is {content root}/wwwroot , but it can be changed with the UseWebRoot method. For more information, see Content root and Web root.

Which of the following Middlewares must be installed to serve static files in ASP.NET Core applications?

To serve static files from an ASP.NET Core app, you must configure static files middleware. With static files middleware configured, an ASP.NET Core app will serve all files located in a certain folder (typically /wwwroot).

What is Microsoft ASP.NET Core server Kestrel?

Kestrel is a cross-platform web server for ASP.NET Core. Kestrel is the web server that's included and enabled by default in ASP.NET Core project templates. Kestrel supports the following scenarios: HTTPS. HTTP/2 (except on macOS†)


2 Answers

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);

like image 98
Jeremy Thompson Avatar answered Sep 18 '22 18:09

Jeremy Thompson


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.

like image 27
joakimriedel Avatar answered Sep 17 '22 18:09

joakimriedel