我正在使用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中的路由本地化
发布评论