ASP.NET Core 2中的路由本地化

编程入门 行业动态 更新时间:2024-10-25 13:14:30
本文介绍了ASP.NET Core 2中的路由本地化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用ASP.NET Core 2开发一个在线商店,并且正在为如何实现路由本地化而苦苦挣扎。取决于用户来自哪个国家/地区,我希望他看到/ en / products或/ pl / produkty。

我设法将文化作为URL的一部分来实现,例如/ en / ....,用户还可以通过单击网站上的按钮来更改默认语言。但是,我不知道如何本地化整个URL。我不想在Startup.cs(MapRoute)中放入数百个网址。我需要一个更好的解决方案,它会在后台自动运行。

如果有人直接更改了网址(例如en / products)并输入pl而不是en,我希望他/她自动重定向到pl / produkty。 / p>

我希望您能帮我!

解决方案

这是一个非常好的资源在这里: Asp.Net核心本地化深入研究

正是您要查找的内容:

IList< CultureInfo> supportCultures = new List< CultureInfo> {新的CultureInfo( en-US),新的CultureInfo( fi-FI),}; var localizationOptions =新的RequestLocalizationOptions { DefaultRequestCulture =新的RequestCulture( en-US), SupportedCultures =支持的文化, SupportedUICultures =支持的文化} ; var requestProvider = new RouteDataRequestCultureProvider(); localizationOptions.RequestCultureProviders.Insert(0,requestProvider); app.UseRouter(routes => { route.MapMiddlewareRoute( {culture = en-US} / {* mvcRoute},subApp => { subApp.UseRequestLocalization(localizationOptions); subApp.UseMvc(mvcRoutes => { mvcRoutes.MapRoute( name:默认,模板: {culture = zh-CN} / {controller = Home} / {action = Index} / {id?}); }); }) ; });

I am developing an online store using ASP.NET Core 2 and I am struggling with how to implement route localization, ex. depending from the country where user is from I want him to see /en/products or /pl/produkty.

I managed to implement culture as part of the url, like /en/...., and user can also change default language by clicking a button on the website. However, I have no idea how to localize whole urls. I don't want to put hundreds of urls in Startup.cs (MapRoute). I need a better solution, which is working automatically behind the scenes.

If someone change directly the url (ex. en/products) and put pl instead of en, I want him/her to be redirected to pl/produkty automatically.

I hope you can help me!

解决方案

here's a very good ressource here: Asp.Net core Localization deep dive

Precisely here's what you're looking for:

IList<CultureInfo> supportedCultures = new List<CultureInfo> { new CultureInfo("en-US"), new CultureInfo("fi-FI"), }; var localizationOptions = new RequestLocalizationOptions { DefaultRequestCulture = new RequestCulture("en-US"), SupportedCultures = supportedCultures, SupportedUICultures = supportedCultures }; var requestProvider = new RouteDataRequestCultureProvider(); localizationOptions.RequestCultureProviders.Insert(0, requestProvider); app.UseRouter(routes => { routes.MapMiddlewareRoute("{culture=en-US}/{*mvcRoute}", subApp => { subApp.UseRequestLocalization(localizationOptions); subApp.UseMvc(mvcRoutes => { mvcRoutes.MapRoute( name: "default", template: "{culture=en-US}/{controller=Home}/{action=Index}/{id?}"); }); }); });

更多推荐

ASP.NET Core 2中的路由本地化

本文发布于:2023-11-14 13:31:10,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1587346.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:路由   ASP   NET   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!