2009-04-29 18 views
0

ruby​​ on railsでルートのプレフィックスを設定する方法については、thisの記事を読んでいます。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 

任意のヒント:私はasp.netのMVCと

を同じことを行うことができるようにしたいので、私は次のようにルートを定義することができるようにしたいですか?

EDIT:

"写真家/ {ID} /写真/ {PHOTOID}" - 私は考えかなりOK仕事をするようだが、どのように私は

​​

をサポートすることができます/写真家/ 1 /写真/追加

答えて

3

このようなあなたのルートを定義します。

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

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

次に、このようなあなたのコントローラのアクションを定義します

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) }); 
関連する問題