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?}");
});
}
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.
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).
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†)
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);
I am unable to reproduce your error using a fresh ASP.NET Core 2.0 project following these steps;
css
in wwwroot.site.css
in wwwroot/css
.app.UseStaticFiles();
at the start of Startup.Configure()
method.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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With