2009-01-04 193 views
5

我试图弄清楚如何处理以下情形。一般来说,我有一堆表中的记录。所有这些都有ID和ParentID字段来组成一棵树。ASP.NET MVC页面/子页面路由

Page1 
- Page2 
- Page3 
Page4 
- Page5 
-- Page6 

现在,我想我的第3页和第6页的路线是像/Page1/Page6/Page3/Page5/Page6 respectivelly。也就是说,我想在URL中包含所有父母。

如何设置我的控制器动作/路由来实现上述结果?

编辑:忘了提,上述结构将是动态的 - 节点可以添加/删除/修改父母等

+0

看看[这](http://stackoverflow.com/questions/296284/mvc-dynamic-routes)>> http://stackoverflow.com/questions/296284/mvc-dynamic-路线 – Mark79 2009-01-13 14:20:41

回答

3

你可以使用通配符,请参阅:http://www.vergentsoftware.com/blogs/ckinsman/ASPNETMVCWildcardRoutes.aspx

一种可能途径:

routes.MapRoute("SomeName", "{*Page}", new { controller = "ControllerName", action = "ActionName" }); 

,并在行动接受字符串页,手动解析它,也许有分流?

编辑:这也可能是有用的:http://weblogs.asp.net/scottgu/archive/2007/12/03/asp-net-mvc-framework-part-2-url-routing.aspx

+1

谢谢,帮了很多。节省了我的时间。 – 2016-09-21 14:15:02

0

注意控制器名称和作用是固定的,而不是根据网址。

// place the more specific route first 
routes.MapRoute("r1", "{parent}/{child}/{subchild}", 
    new { controller = "Page", action = "Index", parent = parent, child = child, subchild = subchild }); 

routes.MapRoute("r2", "{parent}/{child}", 
    new { controller = "Page", action = "Index", parent = parent, child = child }); 


public class PageController 
{ 
    public ActionResult Index(string parent, string child, string? subchild) 
    { 
     // stuff 
    } 
}