2010-11-13 131 views
0

我試圖定義一個路由配置,這將使一個可選的「區域」在下面的網址,所有這些都將默認主頁:如何解決這個路由配置?配置的路由返回404

/uk/home // where the 'uk' parameter can be either 'uk' or 'us' 
/uk  // where the 'uk' parameter can be either 'uk' or 'us' 
/  // in this case, I just want the region to default to 'uk' 

結果我雖然變得不理想。第一個(/uk/home),而第三個(/)都工作,但第二個(/uk),返回404

的配置被定義爲:

 routes.MapRoute(
      null, 
      "{region}/{controller}", 
      new { region = "^UK|US$" }, 
      new { controller = "Home", action = "Index" } 
      ); 

     routes.MapRoute(
      null, 
      "{region}", 
      new { region = "^UK|US$" }, 
      new { controller = "Home", action = "Index" } 
      ); 

     routes.MapRoute(
      null, 
      //"{region}", 
      "", 
      new {region = "UK", controller = "Home", action = "Index" } 
      ); 

我需要做什麼來確保所有3個URL都默認爲主頁,空URL將默認地區設爲'英國'?

回答

1

嘗試以下途徑:

routes.MapRoute(
    "Region", 
    "{region}/{controller}", 
    new { controller = "Home", action = "Index" }, 
    new { region = "^UK|US$" } 
); 

routes.MapRoute(
    "Default", 
    "", 
    new { controller = "Home", action = "Index", region = "UK" } 
);