5

我试图弄清楚如何处理以下情况。一般来说,我在一个表中有一堆记录。所有这些都有 ID 和 ParentID 字段以形成树。

Page1
 - Page2
 - Page3
Page4
 - Page5
 -- Page6

现在,我希望我的 Page3 和 Page6 的路线分别是一样/Page1/Page6/Page3/Page5/Page6。也就是说,我想在 URL 中包含所有父母。

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

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

4

3 回答 3

3

您可以使用通配符匹配。

可能的路线:

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

并在操作中接受字符串 Page,并手动解析它,也许有一个拆分?

这也可能有用:URL 路由

于 2009-01-04T15:52:15.153 回答
0

请注意,控制器名称和操作是固定的,而不是基于 url。

// 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
    }
}
于 2009-01-04T16:28:57.917 回答
-2

你应该使用

routes.MapRoute("Page6", "Page3/Page5/Page6", new { controller = "Page6", action = "Index" });

或者也许使用正则表达式路由

http://blog.sb2.fr/post/2009/01/03/Regular-Expression-MapRoute-With-ASPNET-MVC.aspx

于 2009-01-04T14:31:11.243 回答