我有两个ASP .Core MVC应用程序托管在同一个url下。
我已经设法用Nginx将它们分开,这样一条特定的路径就会去app-2
,而剩下的路径就会去app-1
http://host
-> app-1
http://host/setup
-> app-2
我的问题出现在用户连接到app-2
时,因为应用程序仍然认为它的应用根是http://host
。
这导致客户端在下载样式表时遇到404,因为app-2.css
存在于http://host/setup/css
下,但应用程序在http://host/css
中搜索。
app-2
中.cshtml
文件中的"include"-lines如下所示:
<link rel="stylesheet" type="text/css" href="@Url.Content("~/css/app-2.css")" asp-append-version="true" />
有没有什么方法可以“覆盖”或者告诉app-2
~
应该引用<host>/setup/css/
而不是<host>/css/
?
我真的不想硬编码它,以防url在某一时刻发生变化。
发布于 2021-10-19 11:45:45
经过几个小时的搜索,我发现没有办法更改整个way服务器的应用程序根目录。
我最终做的是创建带有选项的PathHelper
类,并将其添加到Startup.cs
中
class PathHelper
{
public PathHelper(IOptions<PathHelperOptions> opt)
{
Path = opt.Path;
if (Path.StartsWith('/'))
{
Path = Path[1..];
}
if (!Path.EndsWith('/'))
{
Path = Path + '/';
}
}
public string Path { get; }
}
class PathHelperOptions
{
public string Path { get; set; }
}
# Startup.cs
public void ConfigureServices(IServiceCollection services)
{
services
.AddScoped<PathHelper>()
.Configure<PathHelperOptions>(opt =>
{
opt.Path = this.configuration.GetSection("URL_SUFFIX");
});
[...]
}
然后我在.cshtml
文件中使用它,如下所示:
@inject PathHelper helper
<link rel="stylesheet" type="text/css" href="@Url.Content(helper.Path + "css/app-2.css")" asp-append-version="true" />
发布于 2021-10-19 11:57:38
我认为最简单的方法是在来自'app-2‘的页面中包含base
标签。
像这样试一下:
<html>
<head>
<base href="http://host/setup">
</head>
现在你的相关链接被发送到'app-2‘。
https://stackoverflow.com/questions/69613080
复制相似问题