2014-01-20 30 views
2

我跟着this指南,它适用于我的语言支持。网址如 http://domain.com:33982/en-us/Test效果很好。ASP.Net MVC 5路由与文化的网址

但是,我的问题是,我希望它与 http://domain.com:33982/Test以及。我无法弄清楚如何路由它没有文化的链接... 我在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 } 
); 

我真的不明白为什么它不工作。当我去/测试只是将我重定向到默认主页/索引。

有什么建议吗?谢谢

+0

如果你不设置文化违约的默认路由吗? –

+0

请参阅[ASP.NET MVC 5在路由和URL中的文化](http://stackoverflow.com/a/32839796/181087) – NightOwl888

回答

4

这是因为第一和第二路线都匹配。可选参数使应用程序选择匹配的第一个:所以第一个总是被选中。

试试这个:

 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 } 
     ); 

它的工作原理在这里(他们身后retulting文化):

http://host/ (en-us) 
http://host/Home/Index (xx-xx) 
http://host/zh-cn/Home/Index (zh-cn) 
+0

真的不想将Test(又名我的所有控制器的名称)添加到route.cs 。真的想在这种情况下使用{controller}。 – robertk

+0

只是一个例子来向你展示匹配引擎是问题。你必须做一些事情来使路由引擎做出正确的决定。更新的例子。 –

+0

我看到你的例子,是不是有DefaultCulture的任何方式?我真的希望它与domain.com/{controller}一起工作,并且不希望它只是因为文化不存在而重定向到错误页面... – robertk