HttpPost路由破坏映射

时间:2015-06-11 09:38:45

标签: asp.net-mvc routing mvcsitemapprovider

我有一个三层视图路径,我正在尝试路由和映射。

[Route("parent", Name = "parent")]
[MvcSiteMapNode(Title = "Parent", ParentKey = "home", Key = "parent")]
public ActionResult parent()
{
    ...............
}

[Route("parent/child/{param}", Name = "child")]
[MvcSiteMapNode(Title = "Child", ParentKey = "parent", Key = "child", PreservedRouteParameters = "param")] 
public ActionResult child(int param)
{
    ..............
}

[Route("parent/child/{param}/{filterparam1 ?}/{filterparam2 ?}")]
[HttpPost]
public ActionResult child(int param, int filterparam1 = 0, int filterparam2 = 0 )
{
    ..............
}

[Route("parent/child/grandchild/{param}/{name}")]
[MvcSiteMapNode(Title = "Grandchild", ParentKey = "child", Key="grandchild", PreservedRouteParameters = "param, name")]
public ActionResult grandchild(int param, string = name)
{
    ..............

    var node = SiteMaps.Current.CurrentNode;
    node.Title = model.name;

    ..............
}

我遇到的问题是如果我在[HttpPost]上加入[Route]标签:

[Route("parent/child/{param}/{filterparam1 ?}/{filterparam2 ?}")]
[HttpPost]

我的回发过滤器有效,但孙子没有包含在路由树中,没有显示在我的痕迹和节点代码上错误:

var node = SiteMaps.Current.CurrentNode;
node.Title = model.name;

但是,如果我注释掉[HttpPost]上的[Routing]标签,那么过滤器不起作用,但孙子会被路由,显示在我的痕迹上并且节点代码成功运行。

有谁知道发生了什么事?

1 个答案:

答案 0 :(得分:0)

我为有同样问题的人解决了这个问题:

我重新排序了控制器中的代码,以便所有命名路由都在任何帖子之前:

[Route("parent/child/{param}", Name = "child")]
[MvcSiteMapNode(Title = "Child", ParentKey = "parent", Key = "child", PreservedRouteParameters = "param")] 
public ActionResult child(int param)
{
    ..............
}

[Route("parent/child/grandchild/{param}/{name}")]
[MvcSiteMapNode(Title = "Grandchild", ParentKey = "child", Key="grandchild", PreservedRouteParameters = "param, name")]
public ActionResult grandchild(int param, string = name)
{
    ..............

    var node = SiteMaps.Current.CurrentNode;
    node.Title = model.name;

    ..............
}

[Route("parent/child/{param}/{filterparam1 ?}/{filterparam2 ?}")]
[HttpPost]
public ActionResult child(int param, int filterparam1 = 0, int filterparam2 = 0 )
{
    ..............
}