2009-04-29 8 views
0

J'ai lu this article sur la façon dont vous pouvez préfixer les routes en ruby ​​sur rails. Je veux être capable de faire la même chose avec asp.net mvcchemin_prefix pour asp.net routes mvc

Je veux être en mesure de définir un itinéraire comme:

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

Des conseils?

EDIT:

"photographes/{id}/photos/{PHOTOID}" - semble faire le travail tout à fait ok, mais comment puis-je soutenir

RedirectToAction<PhotosController>(x => x.Add()); 

Je voudrais comme rediriger vers:/photographes/1/photos/ajouter

Répondre

3

Définir votre itinéraire comme ceci:

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

Définissez ensuite votre action de commande comme ceci:

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

Vous pouvez utiliser regex routing ou utiliser wildcards in your routing table afin que le {id *} correspond à la /1/photos/2 pour la pho tographers contrôleur par défaut, analyser la chaîne et rediriger vers une action appropriée.

Jetez également un coup d'œil à this sur les ressources imbriquées.

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