Asp.net 如何在Apache虚拟目录中为.NET核心应用程序创建URL
ASP.NET核心应用程序是使用Visual Studio 2019 ASP.NET核心应用程序项目模板创建的 它的_layout.cshtml包含相对于应用程序根目录的css文件路径:Asp.net 如何在Apache虚拟目录中为.NET核心应用程序创建URL,asp.net,apache,asp.net-core,kestrel-http-server,mod-proxy-html,Asp.net,Apache,Asp.net Core,Kestrel Http Server,Mod Proxy Html,ASP.NET核心应用程序是使用Visual Studio 2019 ASP.NET核心应用程序项目模板创建的 它的_layout.cshtml包含相对于应用程序根目录的css文件路径: <head> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0" /
<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>
当应用程序在VisualStudio下运行时,它会工作。当应用程序在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?}");
});
}
问题可能与您定义的UseStaticFiles和UseAuthentication的顺序有关。将UseStaticFiles移到UseAuthentication上方。
更好的方法是使用Apache本身来呈现静态文件。更改顺序后,css字段被找到,但隐私控制器调用仍然会出现404错误。我更新了ProxyPass和ProxyPassReverse之后删除斜杠的问题。
ProxyPasshttp://127.0.0.1:5000/nettest
(不带斜杠)工作正常。非常感谢。
<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>
<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>
public IActionResult Privacy()
{
return View();
}
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?}");
});
}