ASP.NET Core / Kestrel 未始终如一地提供静态内容

ASP.NET Core / Kestrel not serving static content consistently

背景信息:

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>

wwwroot/的部分布局:

已解决。

原来我在 运行 宁 dotnet aspSample.dll 时没有注意我所在的目录 'in'。这样做的结果是我的 'Content root path' 会根据执行该命令时的位置而改变。为了解决这个问题,我必须确保我在 Ubuntu VM 上的正确目录中,在我的例子中是 /publish/then 运行 dotnet aspSample.dll 以确保 Content root path: /var/www/aspSample/publish 设置正确

在查看关于此 post 的未被接受的答案后: 我看到了我的答案,但描述得更好,所以它被引用在下面。

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.