Asp.net路线有两条路线

时间:2015-09-11 12:21:35

标签: asp.net-mvc

我有两个具有相同签名的路由,但只有参数名称不同如何解决此问题。 以下是我的代码

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);

 routes.MapRoute(
     name: "DefaultRoute2",
     url: "{controller}/{action}/{formSubmissionId}",
     defaults: new { controller = "Employee", action = "Index", formSubmissionId = "formSubmissionId" }
 );

2 个答案:

答案 0 :(得分:1)

路由旨在接受不同的URL结构。你的两条路线都有相同的结构,所以第一条路线总是匹配,第二条路线永远不会被测试。

/Employee/Index中,您应该只使用参数id,而不是使用其他路线。

public class EmployeeController : Controller
{
    public ActionResult Index(string id)
    {
        string formSubmissionId = id;
    }
}

该操作的网址与您希望通过第二条路线实现的相同(我相信):Employee/Index/id

<强>更新

我刚刚意识到。如果您只需要参数formSubmissionId用于操作/Employee/Index,则可以执行以下操作:

// Note the order of the routes:
routes.MapRoute(
     name: "DefaultRoute2",
     url: "Employee/Index/{formSubmissionId}",
     defaults: new { controller = "Employee", action = "Index", formSubmissionId = "formSubmissionId" }
 );

routes.MapRoute(
    name: "Default",
    url: "{controller}/{action}/{id}",
    defaults: new { controller = "Account", action = "Login", id = UrlParameter.Optional }
);

public class EmployeeController : Controller
{
    public ActionResult Index(string formSubmissionId)
    {
        // ...
    }
}

答案 1 :(得分:0)

现在我将此问题修复为

    routes.MapRoute(
            name: "DefaultActivity",
            url: "{controller}/LoadActivity/{formSubmissionId}",
            defaults: new { controller = "{controller}", action = "LoadActivity", formSubmissionId = UrlParameter.Optional }
        );

这可以解决我的问题。