ASP.NET Core / Kestrel not serving static content consistently

Viewed 1026

Background Info:

  • ASP.NET Core v3.1 w/ RazorPages
  • Works fine locally (Win10 & Kestrel on localhost)
  • Returns 404 for files in wwwroot/ only on deployment to Linux VM (Ubuntu 18), Bootstrap works fine locally, no 404s
  • Publishing with dotnet publish -r linux-x64
  • The deployed app is running Kestrel and being forwarded requests from NGINX.

Startup.cs

public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
    /// Included before other middleware (needed due to nginx forwarding)
    /// Per: https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-nginx?view=aspnetcore-3.1
    app.UseForwardedHeaders(new ForwardedHeadersOptions
    {
        ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
    });

    if (env.IsDevelopment())
    {
        app.UseDeveloperExceptionPage();
    }
    else
    {
        app.UseExceptionHandler("/Error");
        app.UseHsts();
    }

    app.UseHttpsRedirection();
    app.UseStaticFiles();
    app.UseRouting();

    app.UseCookiePolicy(); // -- Added for AaaS?

    app.UseAuthentication();
    app.UseAuthorization();

    app.UseEndpoints(endpoints =>
    {
        // Added to allow controllers
        endpoints.MapControllers();

        // Original Razor Page way
        endpoints.MapRazorPages();
    });

}

_Layout_.cshtml

<head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>@ViewData["Title"] - Title</title>
    <link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
    <link rel="stylesheet" href="~/css/site.css" />
</head>

partial layout of wwwroot/:

  • css/
    • site.css
  • js/
    • site.js
  • lib/
    • bootstrap/
      • dist/
      • css/
        • bootstrap.css
    • jquery/
1 Answers

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.

Related