切换到{controller}/{id}/{action}会中断RedirectToAction

Eri*_*c P 9 asp.net url-routing asp.net-mvc-routing asp.net-mvc-3

我正在尝试使用适当的REST网址MVC.为此,我切换了默认路由:

{controller}/{action}/{id}
Run Code Online (Sandbox Code Playgroud)

{controller}/{id}/{action}
Run Code Online (Sandbox Code Playgroud)

而不是:

/Customer/Approve/23
Run Code Online (Sandbox Code Playgroud)

现在有

/Customer/23/Approve
Run Code Online (Sandbox Code Playgroud)

ActionLink似乎工作正常,但CustomerController中的以下代码:

[CustomAuthorize]
[HttpGet]
public ActionResult Approve(int id)
{
    _customerService.Approve(id);
    return RedirectToAction("Search");  //Goes to bad url
}
Run Code Online (Sandbox Code Playgroud)

最终在网址上/Customer/23/Search.虽然它应该去/Customer/Search.不知怎的,它记得23 (id).

这是我在global.cs中的路由代码

    routes.MapRoute(
        "AdminRoute", // Route name
        "{controller}/{id}/{action}", 
        new { controller = "Home", action = "Index", id = UrlParameter.Optional },
        new { id = new IsIntegerConstraint() }
        );

    routes.MapRoute(
        "Default", 
        "{controller}/{action}", 
        new { controller = "Home", action = "Index" });
Run Code Online (Sandbox Code Playgroud)

如果我切换这两个功能,RedirectToAction开始工作,但使用:

Html.ActionLink("Approve", "Approve", new { Id = 23})
Run Code Online (Sandbox Code Playgroud)

现在生成/Customer/Approve?id=23,而不是/Customer/23/Approve.

我可以指定直接网址~/Customer/23/Approve,而不是使用ActionLinkRedirectToAction,但宁可坚持使用提供的功能MVC.

mar*_*are 1

尝试在控制器中传递新的(空)RouteValueDictionary

return RedirectToAction("Search", new System.Web.Routing.RouteValueDictionary{});
Run Code Online (Sandbox Code Playgroud)

和这里:

Html.ActionLink("Approve", "Approve", new { Id = 23})
Run Code Online (Sandbox Code Playgroud)

我什至不知道它如何获取客户控制器,因为您没有在任何地方指定它。尝试向 ActionLink 帮助程序提供控制器和操作。