带有 dotnet 核心的 Angular 5 Universal (SSR) 不适用于 Staging 服务器上的 i18n 角度工具

Jea*_*ois 5 internationalization angular-ui-router server-side-rendering angular-universal

我正在尝试使用 I18n Angular 工具运行带有 dotnet 核心的 Angular 项目模板。(https://docs.microsoft.com/en-us/aspnet/core/spa/angular?tabs=visual-studio

这是我在创业课上的内容

app.Map("/fr", fr =>
{
    fr.UseSpa(spa =>
    {
        // To learn more about options for serving an Angular SPA from ASP.NET Core,
        // see https://go.microsoft.com/fwlink/?linkid=864501

        spa.Options.SourcePath = "ClientApp";

        spa.UseSpaPrerendering(options =>
        {
            options.BootModulePath = $"{spa.Options.SourcePath}/dist-server/fr/main.bundle.js";
            options.BootModuleBuilder = env.IsDevelopment()
                ? new AngularCliBuilder(npmScript: "build:ssr2:fr")
                : null;
            options.ExcludeUrls = new[] { "/sockjs-node" };
        });

        if (env.IsDevelopment())
        {
            spa.UseAngularCliServer(npmScript: "serve:fr");
        }
    });
});


app.Map("/en", en =>
{
    en.UseSpa(spa =>
    {
        // To learn more about options for serving an Angular SPA from ASP.NET Core,
        // see https://go.microsoft.com/fwlink/?linkid=864501

        spa.Options.SourcePath = "ClientApp";

        spa.UseSpaPrerendering(options =>
        {
            options.BootModulePath = $"{spa.Options.SourcePath}/dist-server/en/main.bundle.js";
            options.BootModuleBuilder = env.IsDevelopment()
                ? new AngularCliBuilder(npmScript: "build:ssr2:en")
                : null;
            options.ExcludeUrls = new[] { "/sockjs-node" };
        });

        if (env.IsDevelopment())
        {
            spa.UseAngularCliServer(npmScript: "serve:en");
        }
    });
});
Run Code Online (Sandbox Code Playgroud)

这是我的构建命令

"build:ssr2:en":           "npm run buildssr-i1n8-en-browser && npm run buildssr-i1n8-en-server",
"buildssr-i1n8-en-browser":"ng build --aot --locale=en --i18n-file src/i18n/messages.en.xlf --base-href=/en/ --deploy-url=/en/ --output-path=dist/en ",
"buildssr-i1n8-en-server": "ng build --aot --locale=en --i18n-file src/i18n/messages.en.xlf --output-path=dist-server/en --app=ssr --output-hashing=media",

"build:ssr2:fr":           "npm run buildssr-i1n8-fr-browser && npm run buildssr-i1n8-fr-server",
"buildssr-i1n8-fr-browser":"ng build --aot --locale=fr --i18n-file src/i18n/messages.fr.xlf --base-href=/fr/ --deploy-url=/fr/ --output-path=dist/fr",
"buildssr-i1n8-fr-server": "ng build --aot --locale=fr --i18n-file src/i18n/messages.fr.xlf --output-path=dist-server/fr --app=ssr --output-hashing=media",

"serve:fr":                "ng serve --aot --i18n-file=src/i18n/messages.fr.xlf --locale=fr --i18n-format=xlf --base-href=/fr/ ",
"serve:en":                "ng serve --aot --i18n-file=src/i18n/messages.en.xlf --locale=en --i18n-format=xlf --base-href=/en/ ",
Run Code Online (Sandbox Code Playgroud)

当我从 Visual Studio 在 IIS Express 中运行它时,两种语言都可以工作
http://localhost:59508/en/
为法语工作
http://localhost:59508/fr/工作





在生产服务器上,没有任何工作,即使我只放了英文映射。我收到这个错误

发生了未处理的异常:SPA 默认页面中间件无法返回默认页面“/index.html”,因为找不到,并且没有其他中间件处理该请求。您的应用程序正在生产模式下运行,因此请确保它已发布,或者您已手动构建 SPA。或者,您可能希望切换到开发环境。

该文件在 dist/fr , dist/en 和 dist-server/fr, dist-server/en 中很好地生成

知道为什么它在我的生产服务器上不起作用吗?非常感谢

Jea*_*ois 3

哇,这很简单,只需将其添加到我的启动课程中即可

spa.Options.DefaultPage = $"/en/index.html"; // 对于英语

spa.Options.DefaultPage = $"/fr/index.html"; // 法语