Angular 6多语言和.NET Core 2.1

时间:2018-07-19 22:04:47

标签: angular .net-core-2.1

我正在尝试在.NET Core 2.1中制作一个Angular多语言应用程序 为别人服务。我使用app.Map在Startup.cs中运行不同的npmScripts

app.Map("/fr-FR", spaFr =>
{
    spaFr.UseSpa(spa =>
    {
        spa.Options.SourcePath = "ClientApp";
        spa.Options.DefaultPage = $"/fr-FR/index.html";
        if (env.IsDevelopment())
        {
            spa.UseAngularCliServer(npmScript: "start:fr"); 
        }
    });
});
app.UseSpa(spa =>
{
     spa.Options.SourcePath = "ClientApp";
     spa.Options.DefaultPage = $"/index.html";
     if (env.IsDevelopment())
     {
         spa.UseAngularCliServer(npmScript: "start");
     }
});

所以

http://localhost:52337以英语提供应用程序

http://localhost:52337/fr-FR在法国投放应用

但是当我写http://localhost:52337/fr-FR时,应用程序找不到捆绑文件:styles.a006ff9ecb31fc360851.css,runtime.a66f828dca56eeb90e02.js,polyfills.662173b507c7bb60d452.js和main.ddceca57cc95aeab5c91.js

.NET找不到文件,因为它正在http://localhost:52337/中查找,但是文件位于http://localhost:52337/fr-FR

我的package.json有脚本

"start": "ng serve",
"start:fr": "ng serve --serve-path=/ --base-href=/fr-FR --configuration=fr",

我在配置中添加了angular.json

"production-fr": {
  "fileReplacements": [
    {
      "replace": "src/environments/environment.ts",
      "with": "src/environments/environment.prod.ts"
    }
  ],
  "optimization": true,
  "outputHashing": "all",
  "sourceMap": false,
  "extractCss": true,
  "namedChunks": false,
  "aot": true,
  "extractLicenses": true,
  "vendorChunk": false,
  "buildOptimizer": true,
  "outputPath": "dist/fr-FR/",
  "i18nFile": "src/i18n/messages.fr.xlf",
  "i18nFormat": "xlf",
  "i18nLocale": "fr",
  "i18nMissingTranslation": "error"
},
"fr": {
  "aot": true,
  "outputHashing": "all",
  "serve-path":"/fr-FR/",
  "i18nFile": "src/i18n/messages.fr.xlf",
  "i18nFormat": "xlf",
  "i18nLocale": "fr",
  "i18nMissingTranslation": "error"
}

我认为我可以在脚本中使用--serve-path,-deploy-url,-base-href ...进行所有可能的组合,但是我可以在开发环境中进行这项工作。会发生什么?

1 个答案:

答案 0 :(得分:0)

感谢How to configure ASP.net Core server routing for multiple SPAs hosted with SpaServices

解决方案很简单,请使用serve-path = /,并且不要忘记--base-href中的/ final

所以package.json中的脚本变得像

"scripts": {
    ...
    "start": "ng serve",
    "start:fr": "ng serve --base-href=/fr-FR/ --configuration=fr --serve-path=/",
    ...
  },