2011-11-02 36 views
1

我希望能夠訪問同一頁面兩種不同的方式:創建兩個路線選擇到同一個頁面(MVC)

首先,用一個參數,顯示一些特定的信息。

 routes.MapRoute(
      "About", 
      "About/{id}", 
      new { controller = "About", action = "Index" } 
     ); 

二,沒有參數,顯示一般的東西。

 routes.MapRoute(
      "About", 
      "About", 
      new { controller = "About", action = "Index" } 
     ); 

我該如何構建接受這兩個選項的路線?

回答

2
routes.MapRoute(
    "About", 
    "About/{id}", 
    new { controller = "About", action = "Index", id = UrlParameter.Optional } 
); 

然後:

public ActionResult Index(string id) 
{ 
    // if id = null => /About was requested 
    // if id != null => /About/abc was requested 
    ... 
} 
相關問題