Angular 在同一基础上使用多个角度构建配置时更正引用href
我有一个带有ASP.NET核心后端的Angular项目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
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
我有几种方法可以解决这个问题,但没有一种是完全令人满意的
csv
文件并对其进行解析;如果csv文件不是项目的一部分,因此在测试环境中不可用,则无法对其进行测试baseHref
,并在引用中使用相对路径。这会导致整个应用程序中的所有URL都以baseHref作为前缀。这是不可取的,因为我希望多个用户能够互相发送链接以访问同一页面,同时仍然使用用户的首选语言。此外,带有www.example.com/prod/en/…
的url非常难看environment.ts
中设置我必须在每个引用中使用的变量。非常乏味李>