ASP.NET MVC Core 身份页面中的路由本地化 [英] Routed localization in identity pages in ASP.NET MVC Core
问题描述
我目前正在 ASP.NET MVC Core 中开发一个 Web 应用程序,用户应该在其中注册.这是一个本地化的 Web 应用程序,应该能够为多种语言运行.为了对 SEO 友好,我选择了路由本地化,所以我的 url 看起来像:
I am currently developing a web application in ASP.NET MVC Core where users should register. This is a localized web application that should be able to run for multiple languages. To be SEO friendly, I've chosen for routed localization, so my url's look like: https://localhost:5001/en/Catalogue or https://localhost:5001/fr/catalogue.
To allow this, I added this piece of code in my ConfigureServices method in Startup.cs
services.AddLocalization(options => options.ResourcesPath = "Resources");
services.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
.AddViewLocalization()
.AddDataAnnotationsLocalization();
In my Configure method I added this:
IList<CultureInfo> supportedCultures = new List<CultureInfo>
{
new CultureInfo("en"),
new CultureInfo("fr"),
};
var localizationOptions = new RequestLocalizationOptions
{
DefaultRequestCulture = new RequestCulture("en"),
SupportedCultures = supportedCultures,
SupportedUICultures = supportedCultures
};
var requestProvider = new RouteDataRequestCultureProvider();
localizationOptions.RequestCultureProviders.Insert(0, requestProvider);
app.UseRouter(routes =>
{
routes.MapMiddlewareRoute("{culture=en}/{*mvcRoute}", subApp =>
{
subApp.UseRequestLocalization(localizationOptions);
subApp.UseMvc(mvcRoutes =>
{
mvcRoutes.MapRoute(
name: "areaRoute",
template: "{culture=en}/{area:exists}/{controller=Home}/{action=Index}/{id?}");
mvcRoutes.MapRoute(
name: "default",
template: "{culture=en}/{controller=Home}/{action=Index}/{id?}");
});
});
});
This works like a charm. I can translate my MVC pages in any flavour I want. My problem is with the identiy pages. I added those pages as scaffolded items. Their URL's are pointing to https://localhost:5001/Identity/Account/Register. Trying to access them with https://localhost:44339/en/Identity/Account/Register does not work. How can I implement routed localization with identity pages?
AddAreaFolderRouteModelConvention will do the magic:
services.AddMvc()
.AddRazorPagesOptions(options =>
{
options.Conventions.AddAreaFolderRouteModelConvention("Identity", "/Account/", model =>
{
model.Selectors.ForEach(x =>
{
if (x.AttributeRouteModel.Template.StartsWith("Identity"))
{
x.AttributeRouteModel = new AttributeRouteModel()
{
Order = -1,
Template = AttributeRouteModel.CombineTemplates(("{culture=en-US}"),
x.AttributeRouteModel.Template)
};
}
});
});
});
MSDN page says:
pageName String The page name e.g. /Users/List
The page name is the path of the file without extension, relative to the pages root directory for the specified area. e.g. the page name for the file Areas/Identity/Pages/Manage/Accounts.cshtml, is /Manage/Accounts.
So all pages are actually inside "/Account/", here are some generated links:
/en-us/identity/account/login
/en-us/identity/account/manage/index
/en-us/identity/account/manage/orders
if you don't like "/identity/" inside path, you can do this:
AttributeRouteModel.CombineTemplates(("{culture=en-US}"),
x.AttributeRouteModel.Template.Substring("Identity/".Length)) //<==Substring
Then all links will be:
/en-us/account/login
/en-us/account/manage/index
/en-us/account/manage/orders
这篇关于ASP.NET MVC Core 身份页面中的路由本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!