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?}");
});
}
}
}
None
0 Points
1 Post
how to Localization and friendly routes in 3.1 MVC
Apr 04, 2020 09:34 AM|aratar79@hotmail.com|LINK
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
Contributor
3710 Points
1043 Posts
Re: how to Localization and friendly routes in 3.1 MVC
Apr 06, 2020 09:13 AM|Yongqing Yu|LINK
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:
Here is a detailed tutorial, you can refer to: Developing Multicultural ASP.NET Core 3, 2, 1 Project Using LazZiya.ExpressLocalization
Best Regards,
YongQing.
Please remember to click "Mark as Answer" the responses that resolved your issue.
If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.