locked
how to Localization and friendly routes in 3.1 MVC RRS feed

  • Question

  • User561825458 posted

    Hi,
    I create new project asp.core 3.1 mvc with localization

    how can i use routes like /culture/controller/action

    language switching works well, but with controller/action/culture=en

    and also the default language doesn't work

    my startup.cs

    using System.Globalization;
    using base_project.Models;
    using base_project.Services.Mailing;
    using base_project.Validation;
    using Microsoft.AspNetCore.Builder;
    using Microsoft.AspNetCore.Hosting;
    using Microsoft.AspNetCore.Localization;
    using Microsoft.AspNetCore.Mvc;
    using Microsoft.AspNetCore.Mvc.Razor;
    using Microsoft.Extensions.Configuration;
    using Microsoft.Extensions.DependencyInjection;
    using Microsoft.Extensions.Hosting;
    
    namespace base_project
    {
        public class Startup
        {
            public Startup(IConfiguration configuration)
            {
                Configuration = configuration;
            }
    
            public IConfiguration Configuration { get; }
    
            // This method gets called by the runtime. Use this method to add services to the container.
            public void ConfigureServices(IServiceCollection services)
            {
                services.AddControllersWithViews();
                services.AddScoped<IMailSendLogic, MailSendLogic>();
                /**** LOCALIZATION ****/
                services.AddLocalization(options => options.ResourcesPath = "Resources");
                services.AddMvc().AddViewLocalization(LanguageViewLocationExpanderFormat.Suffix);
                services.AddMvc().AddDataAnnotationsLocalization(options => {
                    options.DataAnnotationLocalizerProvider = (type, factory) =>
                        factory.Create(typeof(SharedErrorMessages));
                });
    
                services.Configure<MvcOptions>(options =>
                {
                    options.ModelMetadataDetailsProviders.Add(new CustomValidationProvider());
                });
    
                services.Configure<RequestLocalizationOptions>(options =>
                {
                    var supportedCultures = new[]
                    {
                        new CultureInfo("ca"),
                        new CultureInfo("es"),
                        new CultureInfo("en")
                    };
    
                    //options.DefaultRequestCulture = new RequestCulture("en-US", "en-US");
                    options.DefaultRequestCulture = new RequestCulture("es");
                    options.SupportedCultures = supportedCultures;
                    options.SupportedUICultures = supportedCultures;
                });
    
                services.Configure<ReCAPTCHASettings>(Configuration.GetSection("GooglereCAPTCHA"));
                
                /**** END LOCALIZATION ****/
            }
    
            // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
            public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
            {
                if (env.IsDevelopment())
                {
                    app.UseDeveloperExceptionPage();
                }
                else
                {
                    app.UseExceptionHandler("/Home/Error");
                }
                app.UseStaticFiles();
    
                app.UseRouting();
    
                app.UseAuthorization();
    
                app.UseRequestLocalization();
    
                //app.UseEndpoints(endpoints =>
                //{
                //    endpoints.MapControllerRoute(
                //        name: "default",
                //        pattern: "{culture=es}/{controller=Home}/{action=Index}/{id?}");
                //});
                app.UseEndpoints(endpoints =>
                {
                    endpoints.MapControllerRoute(
                        name: "default",
                        pattern: "{controller=Home}/{action=Index}/{id?}");
                });
            }
        }
    }

    Saturday, April 4, 2020 9:34 AM

All replies

  • User665608656 posted

    Hi ,

    I'm not very sure what the route format you want to display, please describe clearly.

    And you can use this to make the default routes based on the language:

    app.UseEndpoints(endpoints =>
    {
        endpoints.MapControllerRoute(
            name: "default",
            pattern: "{culture=en}/{controller=Home}/{action=Index}/{id?}");
    });

    Here is a detailed tutorial, you can refer to: Developing Multicultural ASP.NET Core 3, 2, 1 Project Using LazZiya.ExpressLocalization

    Best Regards,

    YongQing.

    Monday, April 6, 2020 9:13 AM