簡體   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