Angular 在同一基础上使用多个角度构建配置时更正引用href

Angular 在同一基础上使用多个角度构建配置时更正引用href,angular,asp.net-mvc,asp.net-core,asp.net-core-mvc,angular8,Angular,Asp.net Mvc,Asp.net Core,Asp.net Core Mvc,Angular8,我有一个带有ASP.NET核心后端的Angular项目angular.json包括每种受支持语言的1种生产配置,以及我用于开发的默认配置,如下所示: "build": { "builder": "@angular-devkit/build-angular:browser", // Default config used in development "options": { "outputPath": "wwwroot/dev", "deployUrl": "/de

我有一个带有ASP.NET核心后端的Angular项目
angular.json
包括每种受支持语言的1种生产配置,以及我用于开发的默认配置,如下所示:

"build": {
  "builder": "@angular-devkit/build-angular:browser",

  // Default config used in development
  "options": {
    "outputPath": "wwwroot/dev",
    "deployUrl": "/dev/",
    "index": "src/index.cshtml",
    "main": "src/main.ts",
    "polyfills": "src/polyfills.ts",
    "tsConfig": "src/tsconfig.app.json",
    "assets": ["src/assets/"]
  },

  // Other configurations
  "configurations": {

    "en": {
      "outputPath": "wwwroot/dist/en/",
      "deployUrl": "/dist/en/",
      "i18nFile": "src/locale/my_app.en.xtb",
      "i18nFormat": "xtb",
      "i18nLocale": "en",
      // ... bunch of production optimization flags not relevant to this question ... //
      "fileReplacements": [{
        "replace": "src/environment/environment.ts",
        "with": "src/environment/environment.en.ts"
      }],
      "assets": ["src/assets"]
    },

    "de": {
      // ... Same as "en" configuration with "en" replaced by "de" ... // 
    },

    "fr": {
      // ... //
    } 

  // More language configurations may be added in the future //
  }
}
<script src="/dev/runtime.js" defer></script><script src="/dev/polyfills-es5.js" nomodule defer></script><script src="/dev/polyfills.js" defer></script><script src="/dev/vendor.js" defer></script><script src="/dev/main.js" defer></script>
应该使用哪种语言取决于登录用户的配置文件首选项。为了实现这一点,ASP.NET核心后端仅为根端点提供以下代码:

public async Task<IActionResult> Index()
{
  if (hostingEnvironment.IsDevelopment())
  {
    // In development, always serve the same index.cshtml irregardless of the user's language
    return View("~/wwwroot/dev/index.cshtml");
  }
  else
  {
    // If in production mode, get the user's language through the authentication server
    var lang = await _userInfo.GetLanguage(CurrentUserId);

    // Try to get the index.cshtml that corresponds to the user's language
    var view = View($"~/wwwroot/dist/{lang}/index.cshtml");
    if (view != null)
      return view;

    // If the application hasn't been built in the user's preferred language, default to English
    return View("~/wwwroot/dist/en/index.cshtml");
  }
}
因为我在配置中设置了
deployUrl
,Angular在
index.cshtml
中设置了对脚本文件(
main.js
vendor.js
等)的正确引用,如下所示:

"build": {
  "builder": "@angular-devkit/build-angular:browser",

  // Default config used in development
  "options": {
    "outputPath": "wwwroot/dev",
    "deployUrl": "/dev/",
    "index": "src/index.cshtml",
    "main": "src/main.ts",
    "polyfills": "src/polyfills.ts",
    "tsConfig": "src/tsconfig.app.json",
    "assets": ["src/assets/"]
  },

  // Other configurations
  "configurations": {

    "en": {
      "outputPath": "wwwroot/dist/en/",
      "deployUrl": "/dist/en/",
      "i18nFile": "src/locale/my_app.en.xtb",
      "i18nFormat": "xtb",
      "i18nLocale": "en",
      // ... bunch of production optimization flags not relevant to this question ... //
      "fileReplacements": [{
        "replace": "src/environment/environment.ts",
        "with": "src/environment/environment.en.ts"
      }],
      "assets": ["src/assets"]
    },

    "de": {
      // ... Same as "en" configuration with "en" replaced by "de" ... // 
    },

    "fr": {
      // ... //
    } 

  // More language configurations may be added in the future //
  }
}
<script src="/dev/runtime.js" defer></script><script src="/dev/polyfills-es5.js" nomodule defer></script><script src="/dev/polyfills.js" defer></script><script src="/dev/vendor.js" defer></script><script src="/dev/main.js" defer></script>

因此,角度代码被加载,并正常工作。然而,无论我在哪里引用资产,它都找不到。例如,这样的代码:
将不起作用,因为我必须在src前面加上deployUrl

我有几种方法可以解决这个问题,但没有一种是完全令人满意的

  • 将资产文件夹直接放在wwwroot中,而不是在生成过程中复制它。这意味着wwwroot中的某些文件将受源代码控制,而某些文件将被忽略。这也使得测试更加困难。一些代码实际上从资产中加载一个
    csv
    文件并对其进行解析;如果csv文件不是项目的一部分,因此在测试环境中不可用,则无法对其进行测试
  • 在angular.json中使用
    baseHref
    ,并在引用中使用相对路径。这会导致整个应用程序中的所有URL都以baseHref作为前缀。这是不可取的,因为我希望多个用户能够互相发送链接以访问同一页面,同时仍然使用用户的首选语言。此外,带有
    www.example.com/prod/en/…
    的url非常难看
  • environment.ts
    中设置我必须在每个引用中使用的变量。非常乏味
  • 使用ngx翻译,这样我就不必为每种语言使用一个构建。然而,Angular的本机i18n实现IMO有一些优点,我希望保留这些优点 还有其他解决办法吗