【问题标题】:ASP.NET Core / Kestrel not serving static content consistentlyASP.NET Core / Kestrel 不能始终如一地提供静态内容
【发布时间】:2020-05-11 00:15:12
【问题描述】:

背景信息:

  • 带有 RazorPages 的 ASP.NET Core v3.1
  • 在本地运行良好(本地主机上的 Win10 和 Kestrel)
  • 对于 wwwroot/ 中的文件返回 404 在部署到 Linux VM (Ubuntu 18) 时,Bootstrap 在本地工作正常,没有 404s
  • 使用dotnet publish -r linux-x64 发布
  • 已部署的应用程序正在运行 Kestrel 并正在从 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>

wwwroot/的部分布局

  • css/
    • site.css
  • js/
    • site.js
  • lib/
    • 引导程序/
      • 分布/
      • css/
        • bootstrap.css
    • jquery/

【问题讨论】:

    标签: twitter-bootstrap asp.net-core razor http-status-code-404 kestrel-http-server


    【解决方案1】:

    已解决。

    原来我在运行dotnet aspSample.dll 时没有注意我所在的目录。这样做的结果是我的“内容根路径”会根据我在执行该命令时所处的位置而改变。为了解决这个问题,我必须确保我在 Ubuntu VM 上的正确目录中,在我的情况下为 /publish/然后运行 dotnet aspSample.dll 以确保 Content root path: /var/www/aspSample/publish 设置正确

    在查看此帖子中未接受的答案后:Asp.Net Core 2.0-2.2 Kestrel not serving static content 我看到了我的答案,但描述得更好,所以下面引用了它。

    对我来说,问题在于工作目录。尝试使用 dotnet /var/www/project/project.dll 启动应用程序时,我没有注意我所在的目录。当您以这种方式启动应用程序时,它会自动使用您的当前目录作为工作目录。

    当我查看另一个项目的 .service 文件时,我意识到了这一点,该文件指定了 WorkingDirectory:

    ...
    WorkingDirectory=/var/www/project/
    ExecStart=/usr/bin/dotnet /var/www/project/project.dll
    ...
    

    因此,请确保在运行项目时位于正确的目录中,或者确保在 .service 文件中正确设置了 WorkingDirectory。

    【讨论】:

      猜你喜欢
      • 2018-02-20
      • 2016-08-13
      • 1970-01-01
      • 2019-10-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多