【问题标题】:ASP.NET MVC page/subpage routingASP.NET MVC 页面/子页面路由
【发布时间】:2009-01-04 14:28:36
【问题描述】:

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

Page1
 - Page2
 - Page3
Page4
 - Page5
 -- Page6

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

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

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

【问题讨论】:

标签: asp.net-mvc routing


【解决方案1】:

您可以使用通配符匹配。

可能的路线:

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

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

这也可能有用:URL Routing

【讨论】:

  • 谢谢,帮了大忙。节省了我的时间。
  • @MichaelSamteladze np,那是将近八年前的事了 :)
【解决方案2】:

请注意,控制器名称和操作是固定的,而不是基于 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
    }
}

【讨论】:

    【解决方案3】:

    你应该使用

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-05-14
      • 2010-10-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-03
      • 2015-08-26
      相关资源
      最近更新 更多