ASP.NETCoreMVC基于路由的区域化
创始人
2024-09-18 01:00:20
0

ASP.NET Core MVC可以支持基于路由的区域化,这意味着我们可以在不同的区域中为不同的语言提供不同的视图和资源。下面是实现基于路由的区域化的步骤:

1.首先在Startup.cs文件中添加以下代码:

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

services.AddMvc() .AddViewLocalization() .AddDataAnnotationsLocalization(options => { options.DataAnnotationLocalizerProvider = (type, factory) => factory.Create(typeof(SharedResources)); });

2.接下来,在Program.cs文件中添加以下代码:

public static IWebHostBuilder CreateWebHostBuilder(string[] args) => WebHost.CreateDefaultBuilder(args) .ConfigureAppConfiguration((hostingContext, config) => { config.SetBasePath(hostingContext.HostingEnvironment.ContentRootPath); config.AddJsonFile("appsettings.json", optional: true, reloadOnChange: true); config.AddJsonFile($"appsettings.{hostingContext.HostingEnvironment.EnvironmentName}.json", optional: true); config.AddEnvironmentVariables(); }) .ConfigureServices(services => { services.Configure( options => { var supportedCultures = new List { new CultureInfo("en-US"), new CultureInfo("fr-FR"), new CultureInfo("de-DE"), new CultureInfo("es-ES"), new CultureInfo("it-IT"), new CultureInfo("pt-BR"), new CultureInfo("tr-TR"), new CultureInfo("hi-IN") };

                options.DefaultRequestCulture = new RequestCulture("en-US");
                options.SupportedCultures = supportedCultures;
                options.SupportedUICultures = supportedCultures;
                options.RequestCultureProviders.Insert(0, new RouteDataRequestCultureProvider());
            });
        services.AddSingleton();
        services.AddMvc()
            .AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix)
            .AddDataAnnotationsLocalization();

        services.AddRouting(options =>
        {
            options.ConstraintMap.Add("culture", typeof(LanguageRouteConstraint));
        });
    })
    .Configure(app =>
    {

相关内容

热门资讯

重大来袭!爱来掌中宝有没有挂(... 较多好评“微乐万能挂官网”开挂(透视)辅助教程 了解更多开挂安装加(136704302)微信号是一款...
新手必备!心悦透视辅助器免费(... 新手必备!心悦透视辅助器免费(透视)wepoker游戏安装教程(wpk教程确实有挂)这是一款可以让一...
一起来探讨!新道游辅助器透视挂... 一起来探讨!新道游辅助器透视挂(透视)pokemmo脚本最新版(存在挂教程有挂教程);无需打开直接搜...
透视模拟器!好玩贰柒拾辅助(透... 透视模拟器!好玩贰柒拾辅助(透视)werplan透视挂(AI教程有挂讲解);无需打开直接搜索薇:13...
玩家必知教程!新道游辅助器免费... 哈糖大菠萝可以开挂吗是一款专注玩家量身打造的游戏记牌类型软件,在哈糖大菠萝可以开挂吗这款游戏中我们可...
如何分辨真伪!凑一桌关春天怎么... 较多好评“微乐万能挂官网”开挂(透视)辅助教程 了解更多开挂安装加(136704302)微信号是一款...
记者发布!途游辅助器(透视)w... 记者发布!途游辅助器(透视)wpk辅助哪里买(技巧教程证实有挂) 了解更多开挂安装加(1367043...
重磅来袭!四川皮皮辅助(透视)... 重磅来袭!四川皮皮辅助(透视)佛手在线大菠萝为什么都输(存在挂教程了解有挂);无需打开直接搜索微信(...
透视好友!小南娱乐科技(透视)... 大家好,今天小编来为大家解答小南娱乐科技这个问题咨询软件客服可以免费测试直接加微信(13670430...
总算了解!新久久辅助器(透视)... 新久久辅助器 无需打开直接搜索微信:136704302本司针对手游进行,选择我们的四大理由: 1、软...