2009-04-29 84 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) });