Reputation: 455
Successfully using the project laid out at Localized routing using ASP.NET Core MVC 2 however there are a few modifications I'd like to make and I'm not clear on how to go about.
Currently my start.cs looks like the below, this works fine however what it does is in the Default culture English, my route is www.site.com whereas when i switch to any other culture I get www.site.com/fr/accuel or www.site.com/es/casa...
How can I get the Default Language to always appear as www.site.com/en/home
startup.cs
// Set up cultures
LocalizationRouteDataHandler.DefaultCulture = "en";
LocalizationRouteDataHandler.SupportedCultures = new Dictionary<string, string>()
{
{ "en", "English" },
{ "fr", "Français" },
{ "es", "Español" }
};
And my HomeController
[AllowAnonymous]
[LocalizationRoute("en", "home")]
[LocalizationRoute("fr", "accueil")]
[LocalizationRoute("es", "casa")]
public class HomeController : LocalizationController
{
[LocalizationRoute("en", "/home")]
[LocalizationRoute("fr", "/accueil")]
[LocalizationRoute("es", "/casa")]
public IActionResult Index()
{
return View();
}
Upvotes: 0
Views: 241
Reputation: 29986
For LocalizationRoute
, it defines the route template for MVC, which is used to map the request to the action.
For default design, for request /
which will be routed to Home/Index
with english culture. If you prefer to show the url with /en/home
you need to redirect the url by code below:
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
var options = new RewriteOptions()
.AddRedirect(@"^$", $"{LocalizationRouteDataHandler.DefaultCulture}/{LocalizationRouteDataHandler.DefaultController}");
app.UseRewriter(options);
var localizationOptions = app.ApplicationServices.GetService<IOptions<RequestLocalizationOptions>>();
app.UseRequestLocalization(localizationOptions.Value);
//rest code
app.UseMvc(routes =>
{
routes.MapRoute(
name: "default",
template: "{controller=Home}/{action=Index}/{id?}",
defaults: new { culture = LocalizationRouteDataHandler.DefaultCulture }
);
});
}
Note For above way, you need to keep [LocalizationRoute("en", "/home")]
to HomeController.
Upvotes: 1