Routed localization in identity pages in ASP.NET MVC Core

北战南征 提交于 2019-12-23 02:01:34

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/54261042/routed-localization-in-identity-pages-in-asp-net-mvc-core

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!