2009-04-29 83 views
0

我讀this關於如何在Ruby on Rails上加前綴路由的文章。我希望能夠做同樣的事情用asp.net mvc的asp.net mvc路由的path_prefix

所以我想能夠定義諸如路線:

/photographers/1/photos/2 //photo 2 of photographer with id 1 
/photographers/1/photos  //all of photographer with id 1 

任何提示嗎?

編輯:

「攝影記者/ {ID} /照片/ {PHOTOID}」 - 似乎做的工作相當好,但我怎麼能支持

​​

我會喜歡重定向到:/攝影師/ 1 /照片/添加

回答

3

定義您的路線是這樣的:

routes.MapRoute(
    "Photographers", 
    "photographers/{id}/photos/{photoID}", 
    new { controller = "Photographers", action = "Photo", photoID = null }); 

然後這樣定義你的控制器動作:

public ActionResult Photo(int id, int? photoID) 
{ 
    // If photoID is not null, show just that photo. 
    // Otherwise, show all photographs. 
} 
0

您可以使用regex routing或使用wildcards in your routing table,以便{id *}匹配pho的/1/photos/2 tographers默認控制器,解析字符串,並重定向到適當的操作。

另請參閱this關於嵌套資源的文章。

RouteTable.Routes.Add(
     new Route { Url = "events/[eventId]/tickets/[action]/[id]", 
        Defaults = new { controller = "Tickets", 
            action = "List", id = (string)null }, 
        RouteHandler = typeof(MvcRouteHandler) });