ASP.NET Core 应用程序是使用 Visual Studion 2019 ASP.NET Core 应用程序项目模板创建的。
它的 _layout.cshtml 包含相对于应用程序根目录的 css 文件路径:
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>@ViewData["Title"] - WebApplication1</title>
<link rel="stylesheet" href="~/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="~/css/site.css" />
</head>
此应用程序使用https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-apache中的说明在 nettest 虚拟目录中使用 apache 在 Debian Linux 10 中运行
Apache mod_proxy 和标头模块是用户将 https 请求转发到 kestrel 服务器。阿帕奇配置文件:
<Location "/nettest">
RequestHeader set "X-Forwarded-Proto" expr=%{REQUEST_SCHEME}
ProxyPreserveHost On
ProxyPass http://127.0.0.1:5000/
ProxyPassReverse http://127.0.0.1:5000/
</location>
应用程序找不到 css 文件。浏览器视图源码显示绝对路径是在 html 代码中呈现的:
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Home Page - WebApplication1</title>
<link rel="stylesheet" href="/lib/bootstrap/dist/css/bootstrap.min.css" />
<link rel="stylesheet" href="/css/site.css" />
</head>
如何强制呈现正确的 URL,等。相对于当前文件夹,如
lib/bootstrap/dist/css/bootstrap.min.css
或绝对来自应用程序虚拟目录:
/nettest/lib/bootstrap/dist/css/bootstrap.min.css
** 更新 **
更改顺序后找到 css 文件。但是示例应用程序模板中的隐私链接仍然无效。
ASP.NET 模板包含:
<div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Index">Home</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" asp-area="" asp-controller="Home" asp-action="Privacy">Privacy</a>
</li>
</ul>
</div>
在浏览器中显示为
<div class="navbar-collapse collapse d-sm-inline-flex flex-sm-row-reverse">
<ul class="navbar-nav flex-grow-1">
<li class="nav-item">
<a class="nav-link text-dark" href="/nettest">Home</a>
</li>
<li class="nav-item">
<a class="nav-link text-dark" href="/nettest/Home/Privacy">Privacy</a>
</li>
</ul>
</div>
主页链接有效,但隐私链接引发 404 错误。
在模板 HomeController 中定义:
public IActionResult Privacy()
{
return View();
}
它在应用程序在 Visual Studio 下运行时工作。当应用程序在 Apache 虚拟目录中运行时,如何使私有链接工作?
配置方法:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
{
// https://docs.microsoft.com/en-us/aspnet/core/host-and-deploy/linux-apache?view=aspnetcore-2.2
app.UseForwardedHeaders(new ForwardedHeadersOptions
{
ForwardedHeaders = ForwardedHeaders.XForwardedFor | ForwardedHeaders.XForwardedProto
});
app.UseAuthentication();
if (env.IsDevelopment())
{
app.UseDeveloperExceptionPage();
}
else
{
app.UseExceptionHandler("/Home/Error");
// The default HSTS value is 30 days. You may want to change this for production scenarios, see https://aka.ms/aspnetcore-hsts.
app.UseHsts();
}
app.UseHttpsRedirection();
// origoli: app.UseStaticFiles();
// https://stackoverflow.com/questions/46593999/how-to-host-a-asp-net-core-application-under-a-sub-folder
// https://forums.asp.net/post/6327484.aspx
// because the middleware
//app.UsePathBase("/nettest");
// strips the /nettest from the pipeline, the static file handler middleware needs to come first:
app.UseStaticFiles(new StaticFileOptions
{
RequestPath = new PathString("/nettest")
});
app.UsePathBase("/nettest");
// in asp.net core, you control the order of middleware and must get it correct.
app.UseRouting();
app.UseAuthorization();
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller=Home}/{action=Index}/{id?}");
});
}