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

Page1
 - Page2
 - Page3
Page4
 - Page5
 -- Page6

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

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

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

有帮助吗?

解决方案

您可以使用通配符匹配。

可能的路线:

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

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

这也可能有用: 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
    }
}

你应该使用

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top