ASP.Net MVC support for Nested Resources?

*爱你&永不变心* 提交于 2019-11-29 12:31:53

问题


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 = "" });

回答1:


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...



来源:https://stackoverflow.com/questions/1381863/asp-net-mvc-support-for-nested-resources

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!