ASP.NET Core / Kestrel 未始终如一地提供静态内容
ASP.NET Core / Kestrel not serving static content consistently
背景信息:
- ASP.NET Core v3.1 带 RazorPages
- 在本地工作正常(本地主机上的 Win10 和 Kestrel)
- Returns 404 对于 wwwroot/ 中的文件 仅 部署到 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
- 库/
- bootstrap/
- 距离/
- css/
- bootstrap.css
- jquery/
已解决。
原来我在 运行 宁 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.
背景信息:
- ASP.NET Core v3.1 带 RazorPages
- 在本地工作正常(本地主机上的 Win10 和 Kestrel)
- Returns 404 对于 wwwroot/ 中的文件 仅 部署到 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
- 库/
- bootstrap/
- 距离/
- css/
- bootstrap.css
- jquery/
- bootstrap/
已解决。
原来我在 运行 宁 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.