Route localization in ASP.NET Core 2

后端 未结 1 1994
长情又很酷
长情又很酷 2021-02-13 04:42

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 t

相关标签:
1条回答
  • 2021-02-13 05:21

    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?}");
            });
        });
    });
    
    0 讨论(0)
提交回复
热议问题