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?}");
});
}
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…