Reputation: 2461
I followed this guide and it works fine with my language support. Url's such as http://domain.com:33982/en-us/Test works great.
However my issue is that I want it to work with http://domain.com:33982/Test as well. I can't figure it out how to route it without the culture in the link... My routes in the RouteConfig.cs
routes.MapRoute(
name: "Default",
url: "{culture}/{controller}/{action}/{id}",
defaults: new { culture = CultureHelper.GetDefaultCulture(), controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapRoute(
name: "MissingCulture",
url: "{controller}/{action}/{id}",
defaults: new { culture = CultureHelper.GetDefaultCulture(), controller = "Test", action = "PageMissing", id = UrlParameter.Optional }
);
I don't really understand why it doesn't work. When I go to /Test to just redirects me to the default Home/Index.
Any suggestions? Thanks
Upvotes: 2
Views: 5774
Reputation: 156918
That is because the first and second route both match. The optional parameters cause the application to choose the first one that matches: so the first one always gets picked.
Try this:
routes.MapRoute( name: "DefaultWithCulture"
, url: "{culture}/{controller}/{action}/{id}"
, defaults: new { culture = "en-us", controller = "Home", action = "Index", id = UrlParameter.Optional }
, constraints: new { culture = "[a-z]{2}-[a-z]{2}" }
);
routes.MapRoute( name: "Default"
, url: "{controller}/{action}/{id}"
, defaults: new { culture = "xx-xx", controller = "Home", action = "Index", id = UrlParameter.Optional }
);
It works over here for (retulting culture behind them):
http://host/ (en-us)
http://host/Home/Index (xx-xx)
http://host/zh-cn/Home/Index (zh-cn)
Upvotes: 4