惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Threat Intelligence Blog | Flashpoint
Threat Intelligence Blog | Flashpoint
L
LINUX DO - 热门话题
Help Net Security
Help Net Security
AWS News Blog
AWS News Blog
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
NISL@THU
NISL@THU
T
Threat Research - Cisco Blogs
C
CERT Recently Published Vulnerability Notes
C
Cisco Blogs
P
Privacy International News Feed
博客园 - 聂微东
T
Tenable Blog
Recent Announcements
Recent Announcements
D
Darknet – Hacking Tools, Hacker News & Cyber Security
Latest news
Latest news
The GitHub Blog
The GitHub Blog
爱范儿
爱范儿
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
T
The Exploit Database - CXSecurity.com
博客园 - 三生石上(FineUI控件)
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
P
Proofpoint News Feed
Security Archives - TechRepublic
Security Archives - TechRepublic
P
Privacy & Cybersecurity Law Blog
Hugging Face - Blog
Hugging Face - Blog
WordPress大学
WordPress大学
Know Your Adversary
Know Your Adversary
S
Schneier on Security
云风的 BLOG
云风的 BLOG
GbyAI
GbyAI
Stack Overflow Blog
Stack Overflow Blog
W
WeLiveSecurity
G
Google Developers Blog
Microsoft Azure Blog
Microsoft Azure Blog
AI
AI
G
GRAHAM CLULEY
小众软件
小众软件
博客园 - 司徒正美
Scott Helme
Scott Helme
罗磊的独立博客
Project Zero
Project Zero
A
About on SuperTechFans
MyScale Blog
MyScale Blog
L
LangChain Blog
TaoSecurity Blog
TaoSecurity Blog
P
Palo Alto Networks Blog
H
Heimdal Security Blog
N
News and Events Feed by Topic
阮一峰的网络日志
阮一峰的网络日志

博客园 - 风中灵药

如何在mvc中共用一个dbconext以节省资源提高性能? mp3转speex的一些研究(貌似不能播放,暂存着) 安装win7 64位和win10 64位双系统小结 macbook装双系统多分区其实很简单,你只要把macbook当作一台普通pc就可以了! 个人对AutoResetEvent和ManualResetEvent的理解 关于WPF下ComBox的SelectionChanged事件 一条语句实现查询各类别前10条记录 【原】Sql2005 实现递归 【转】javascript操作cookies 以及 正确使用cookies的属性 回归: css, bug bug, background-repeat and frames 如何用oledb读取dbf(FoxPro表)文件? SQL注入中利用XP_cmdshell提权的用法(转) Sql 2005自动备份并自动删除3天前的备份 ActiveX开发心得(原创) 常用正则表达式(包括中文匹配) 使用ASP.NET Global.asax 文件(转) 如何不打开文件 直接出现下载保存提示框 Replace ntext类型中的文字 关于IFRAME 自适应高度的研究[转]
asp.net core api路由及多语言切换的实现
风中灵药 · 2021-11-08 · via 博客园 - 风中灵药

预期目标:https://myweb.com/zh-CN/api/post、https://myweb.com/en-US/api/post

即通过地址实现多语言切换。

在startup.cs中配置:

 1 public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
 2         {
 3             if (env.IsDevelopment())
 4             {
 5                 app.UseDeveloperExceptionPage();
 6             }
 7 
 8             app.UseHttpsRedirection();
 9 
10             app.UseRouting();
11 
12             app.UseAuthorization();
13 
14             app.UseStaticFiles();
15 
16             CultureInfo[] supportedCultures = new[]
17             {
18                 new CultureInfo("en"),
19                 new CultureInfo("zh-CN")
20             };
21 
22             var options = new RequestLocalizationOptions();
23             options.DefaultRequestCulture = new RequestCulture("en");
24             options.SupportedCultures = supportedCultures;
25             options.SupportedUICultures = supportedCultures;
26 
27             options.RequestCultureProviders.Insert(0, new RouteDataRequestCultureProvider { Options = options });
28 
29             app.UseRequestLocalization(options);//一定要放在这个位置
30 
31             app.UseEndpoints(endpoints =>
32             {
33                 //endpoints.MapControllers();
34                 endpoints.MapControllerRoute(name: "default", pattern: "~/{culture}/{controller}/{action}");
35             });
36         }

采用MapControllerRoute配置路由则无需再为controler及其action配置属性路由,controler及其action会自动根据MapControllerRoute配置的共享路由配置自己的路由。如果仍然给controler及其action配置了属性路由,则系统则以ontroler及其action的属性路由为准。换句话说,MapControllerRoute配置的路由是共享路由,controler及其action配置的路由是针对单个controler及其action的个性化路由。个性化路由优先于共享路由。

RouteDataRequestCultureProvider是实现地址栏多语言切换的重要类,配合endpoints.MapControllerRoute的{culture}参数设置即可实现目标,它会根据路由规则自动读取地址栏对应的culture值设置为当前的cultureInfo。

上面的代码通过下面的方式修改,可以达到相同效果。

把16行到27行的代码定义到ConfigureServices(IServiceCollection services)方法中:

 1 public void ConfigureServices(IServiceCollection services)
 2         {
 3             CultureInfo[] supportedCultures = new[]
 4            {
 5              new CultureInfo("en"),
 6              new CultureInfo("zh-CN")
 7             };
 8 
 9             services.Configure<RequestLocalizationOptions>(options =>
10             {
11                 options.DefaultRequestCulture = new RequestCulture("en");
12                 options.SupportedCultures = supportedCultures;
13                 options.SupportedUICultures = supportedCultures;
14 
15                 options.RequestCultureProviders.Insert(0, new RouteDataRequestCultureProvider { Options = options });
16             });
17 
18             
19             services.AddControllers();
20         }

然后把第一段代码的16到29行改为:

app.UseRequestLocalization();

即可。效果是一样的!