繁体   English   中英

ASP .Core MVC 配置应用程序根 url

[英]ASP .Core MVC configure application root url

我有两个 ASP .Core MVC 应用程序,它们托管在同一个 url 下。
我设法用 Nginx 将它们分开,以便某个路径转到app-2 ,而其余​​路径转到app-1
http://host -> app-1
http://host/setup -> app-2

当用户连接到app-2 ,我的问题就出现了,因为应用程序仍然认为它的 app-root 是http://host
这会导致客户端在下载样式表时遇到 404,因为app-2.css存在于http://host/setup/css但应用程序在http://host/css

app-2 .cshtml文件中的“include”行如下所示:

<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 在某些时候发生变化。

经过数小时的搜索,我发现无法更改整个网络服务器的应用程序根目录。
我最终做的是使用选项创建类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" />

我认为最简单的方法是在来自“app-2”的页面中包含base标签。

像这样尝试:

<html>
    <head>
        <base href="http://host/setup">
    </head>

现在您的相关链接被发送到“app-2”。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM