2

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

Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
robertk
  • 2,461
  • 1
  • 27
  • 39
  • what if you dont set a default for culture on the Default route? – DJ. Jan 20 '14 at 16:47
  • See [ASP.NET MVC 5 culture in route and url](http://stackoverflow.com/a/32839796/181087) – NightOwl888 Jul 16 '16 at 20:33
  • Possible duplicate of [ASP.NET MVC 5 culture in route and url](https://stackoverflow.com/questions/32764989/asp-net-mvc-5-culture-in-route-and-url) – jww Dec 18 '18 at 12:12

1 Answers1

4

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)
Patrick Hofman
  • 153,850
  • 22
  • 249
  • 325
  • Really do not want to add Test (a.k.a name of all my controllers) to the route.cs. Really want to use the {controller} in that case. – robertk Jan 20 '14 at 13:25
  • Just an example to show you the matching engine is the problem. You have to do something to make the routing engine make the right decision. Updated example. – Patrick Hofman Jan 20 '14 at 13:26
  • I see your example, is that any way not to have the DefaultCulture there? I really want it to work with domain.com/{controller} and not want it to redirect to error-page just because the culture isn't there... – robertk Jan 20 '14 at 14:03
  • I assume the constraint should be: constraints: new { culture="[a-z]{2}-[a-z]{2}" } becuase the other one is not working (compile error). However it still doesn't seem to work. /en/Test doesn't work. The only way to get it working now is by: "Test/Index", not even /Test/ work. – robertk Jan 20 '14 at 14:33
  • And if you use en-us? – Patrick Hofman Jan 20 '14 at 14:39
  • @PatrickHofman you may want to see mostafa kazemi answer: he was trying to comment on your answer regarding the `constraints`, but didn't had enough reputation for that. – Cœur Dec 18 '18 at 12:30