2010-08-23 217 views
0

傢伙,我有我的asp.net mvc網站的頁面。 的路由配置:ASP.net MVC自定義路由

 public static void RegisterRoutes(RouteCollection routes) 
    { 
     routes.IgnoreRoute("{resource}.axd/{*pathInfo}"); 

     routes.MapRoute(
      "Default", // Route name 
      "{controller}/{action}/{index}", // URL with parameters 
      new { controller = "Home", action = "Detail", index = "" } // Parameter defaults 
     ); 
    } 

控制器代碼:

 public ActionResult Detail(string index) 
    { 
     string[,] List = new string[,] { {"1", "first item"}, {"3", "middle item"}, {"5", "last item"}}; 
     ViewData["Message"] = "no results."; 

     if (!string.IsNullOrEmpty(index)) 
     { 
      for (int i = 0; i <= List.GetUpperBound(0); i++) 
      { 
       if (List[i, 0] == index) 
       { 
        ViewData["Message"] = List[i, 1]; 
       } 
      } 
     } 

     return View(); 
    } 

,我想用戶訪問http://www.domain.com/5重定向動作詳細參數5.

怎麼支持呢?

認爲。

回答

6

嘗試定義之前下列路線默認路由:

routes.MapRoute(
    "Custom", 
    "{index}", 
    new { controller = "Home", action = "Detail", index = "" } 
); 
+0

認爲,這是正確的。 – Net205 2010-08-23 07:09:54

+0

對不起,這是「謝謝」,而不是「認爲」 – Net205 2010-08-23 07:15:51

+0

@Darin Dimitrov我在這裏問了一個類似的問題:http://stackoverflow.com/questions/23237336/mvc-route-map-without-any-routing-你可以檢查一下參數 – 2014-04-23 08:00:21