我正在尝试使用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");
}
});
});
这是我的构建命令
"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/ ",
当我从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中生成
任何想法为什么它在我的生产服务器上不起作用?非常感谢
哇,在我的启动课中添加它很简单
spa.Options.DefaultPage = $“/ en / index.html”; //用于英语
spa.Options.DefaultPage = $“/ fr / index.html”; //为法语
要完成Jean-Francois的问题,这2个设置对我有用
第一:
app.UseSpa(spa =>
{
spa.Options.DefaultPage = $"/hr/index.html"; // Default language to use
});
第二:
app.Map("/en", en =>
{
en.UseSpa(spa =>
{
spa.Options.DefaultPage = $"/en/index.html";
});
});
app.Map("/hr", hr =>
{
hr.UseSpa(spa =>
{
spa.Options.DefaultPage = $"/hr/index.html";
});
});
第一个更好,因为它会自动重定向到angular.json文件中设置的默认语言,并且更短:P。示例使用带有如上所述的内置选项的角度7。技巧是将baseHref设置为文件夹位置。