ActionLink不区分两个不同的类似路由

时间:2013-12-10 21:16:07

标签: c# asp.net-mvc-3 asp.net-mvc-4 razor routes

我有两条路线:

    routes.MapRoute(
        "route1",
        "{controller}/{action}/{param1}/{param2}/NotTest/{notParam}/{param4}",
        new { controller = "Home", action = "Index" },
            new[] { "Controllers" });

        routes.MapRoute(
            "route2",
            "{controller}/{action}/{param1}/{param2}/Test/{param3}/{param4}",
            new { controller = "Home", action = "Index" },
                new[] { "Controllers" });

其中NotTest和Test都是url中的静态字符串,唯一区分两条路由的是notParam和param3是不同的字符串。我可以使用actionlink链接到route2,但是无论何时我尝试链接到route1,我都会向我的控制器发送一个get请求,其中包含一堆参数,而不是使用实际路由进行链接。使用RouteLink我甚至没有获得URL ...

我应该做些什么来区分这两条路线?

我的链接是这样的:

@Html.ActionLink("Example Data", "Action", "Controller", new {param1= Model.Data, param2, param3, notParam, param4}, new { id = "columnsID" })

2 个答案:

答案 0 :(得分:2)

您应该在这里使用Html.RouteLink方法:

@Html.RouteLink("Example Data", 
                "route1",
                new 
                { 
                    action = "Action",
                    controller = "Controller"
                    /*other route values*/
                 })

答案 1 :(得分:1)

您可能需要通过直接引用路线来构建此链接:

<a href="@Url.RouteUrl("route1", new { httproute = "", controller = "Controller", action = "Action", param1 = param1, param2 = param2, notParam = notParam, param4 = param4 })">Your Link</a>

<a href="@Url.RouteUrl("route2", new { httproute = "", controller = "Controller", action = "Action", param1 = param1, param2 = param2, param3 = param3, param4 = param4 })">Your Link</a>