简体   繁体   English

在ASP.NET MVC Core中的身份页面中进行路由本地化

[英]Routed localization in identity pages in ASP.NET MVC Core

I am currently developing a web application in ASP.NET MVC Core where users should register. 我目前正在ASP.NET MVC Core中开发一个用户应该注册的Web应用程序。 This is a localized web application that should be able to run for multiple languages. 这是一个本地化的Web应用程序,应该能够运行多种语言。 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 . 为了SEO友好,我选择了路由本地化,所以我的网址看起来像: https:// localhost:5001 / en / Cataloghttps:// localhost:5001 / fr / catalog

To allow this, I added this piece of code in my ConfigureServices method in Startup.cs 为此,我在Startup.cs的ConfigureServices方法中添加了这段代码

services.AddLocalization(options => options.ResourcesPath = "Resources");

        services.AddMvc()
            .SetCompatibilityVersion(CompatibilityVersion.Version_2_1)
            .AddViewLocalization()
            .AddDataAnnotationsLocalization();

In my Configure method I added this: 在我的Configure方法中,我添加了:

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. 我可以用我想要的任何风格翻译我的MVC页面。 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 . 他们的URL指向https:// localhost:5001 / Identity / Account / Register Trying to access them with https://localhost:44339/en/Identity/Account/Register does not work. 尝试使用https:// localhost:44339 / en / Identity / Account / Register访问它们不起作用。 How can I implement routed localization with identity pages? 如何使用身份页面实现路由本地化?

AddAreaFolderRouteModelConvention will do the magic: AddAreaFolderRouteModelConvention将做出神奇的事:

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: MSDN页面说:

pageName String The page name eg /Users/List pageName字符串页面名称,例如/ Users / List

The page name is the path of the file without extension, relative to the pages root directory for the specified area. 页面名称是相对于指定区域的页面根目录的没有扩展名的文件的路径。 eg the page name for the file Areas/Identity/Pages/Manage/Accounts.cshtml, is /Manage/Accounts. 例如,文件Areas / Identity / Pages / Manage / Accounts.cshtml的页面名称是/ Manage / Accounts。

So all pages are actually inside "/Account/", here are some generated links: 所以所有页面实际上都在“/ Account /”里面,这里有一些生成的链接:

/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: 如果你不喜欢路径内的“/ identity /”,你可以这样做:

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

在此输入图像描述

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM