ASP.NET Core 2.0-2.2 Kestrel Not Serving Static Content

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.

ASP.NET Core / Kestrel not serving static content consistently

Resolved.

Turns out that I wasn't paying attention to the directory that I was 'in' when running dotnet aspSample.dll. The result of this was that my 'Content root path' would change based on where I was when executing that command. To solve the issue, I had to ensure I was in the right directory on the Ubuntu VM, /publish/ in my case, then run dotnet aspSample.dll to ensure that the Content root path: /var/www/aspSample/publish was set correctly

After reviewing the non-accepted answers on this post: Asp.Net Core 2.0-2.2 Kestrel not serving static content I saw my answer, but described better so it's block quoted below.

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.

Why ASP.NET Core web application does not serve static web assets when started from command line?

The question is a bit misleading, as the artifacts in bin/Debug folder is for debugging and not for general testing, so it is not guaranteed to work or supposed to work as you described.

The reason why you found it is not working is because there is no static assets in bin/Debug folder. It works in development environment and not in production (the default when no environment set) because the default builder calls UseStaticWebAssets only in development environment.

https://github.com/dotnet/aspnetcore/blob/4e7d976438b0fc17f435804e801d5d68d193ec33/src/DefaultBuilder/src/WebHost.cs#L221

Inside UseStaticWebAssets, it processes .staticwebssets.runtime.json file to resolve the real path of your static assets. That is how it works and why it does only when environment is development.

If you want to have it works as it supposes to, you should publish your project to a folder and call the executable from there. The publish tool will also publish your static assets into that folder so it will work whether you set the environment config or not.

How to disable the display of loading static files in Miniprofiler for asp.net core 2.0

Make sure you add the MiniProfiler middleware AFTER the StaticFiles middleware in your Startup.cs file:

public void Configure(IApplicationBuilder app) {
app.UseFileServer();
app.UseStaticFiles();
app.UseMiniProfiler();
app.UseMvc();
}

If that is not an option or doesn't solve your problem you can also configure MiniProfiler to ignore the paths where your static files are located:

public IServiceProvider ConfigureServices(IServiceCollection services) {
services.AddMiniProfiler(options => {
options.IgnoredPaths.Add("/js/");
options.IgnoredPaths.Add("/css/");
})
}

Asp.net Core 2.0 Static files not found error

You should place the static files you wish to expose under wwwroot and then reference them accordingly e.g.

See Static files in ASP.NET Core

<link rel="stylesheet" href="~/css/style.css" asp-append-version="true" />

If you want to serve static files outside of the wwwroot then you will need to configure static file middleware as follows:

public void Configure(IApplicationBuilder app)
{
app.UseStaticFiles(); // For the wwwroot folder

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

You can then use similar markup to what you have currently:

<link href="@Url.Content("~/Content/css/style.css")" rel="stylesheet">

See Serve files outside of web root for more information.



Related Topics



Leave a reply



Submit