Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ASP.Net MVC support for Nested Resources?

I'm looking for a routing option similar to the nested RESTFul routes functionality available through Rails. The SimplyRestful project on MvcContrib doesn't appear to be active any longer nor does it appear to be current with the 1.0 MVC release.

This is the uri scheme I'm looking for,

/Activity/10/Task/1/Edit or /Activity/10/Task/Edit/1

I simply haven't been able to get it to work and all the documentation I've run across describes the non-nested scenario. It doesn't seem like it'd be that difficult....

This is what I've been working with...

    routes.MapRoute(null,
        "Activity/{activityId}/Task/{action}/{id}", 
        new { controller = "Task", action = "Edit", activityId = "", id = "" });
like image 746
nerraga Avatar asked Sep 04 '09 22:09

nerraga


1 Answers

Add a default value for activity:

routes.MapRoute(null,
    "Activity/{activityId}/Task/{action}/{id}/{activity}",
    new { 
        controller = "Task", 
        action = "Edit", 
        id = "",
        activityId = "", 
        activity = "" });

Remember also that this route will only pick up urls on the form

/Activity/10/Edit/1/theActivity

while your example

/Activity/10/Edit/1

will be picked up by the default route, setting the route values to

controller = "Activity"
action = "10"
id = "Edit/1"

Phil Haack's Routing Debugger for ASP.NET MVC is extremely useful for figuring out how to work these things...

like image 84
Tomas Aschan Avatar answered Oct 17 '22 08:10

Tomas Aschan