我有这两条路线:
routes.MapRoute(
"Default", // Route name
"{controller}/{action}/{id}", // URL with parameters
new { controller = "Home", action = "Index", id = UrlParameter.Optional } // Parameter defaults
);
routes.MapRoute(
"Default2", // Route name
"{controller}/{action}/{OrderId}/{CustomerID}", // URL with parameters
new { controller = "NorthwindOrders", action = "Index", OrderId = UrlParameter.Optional, CustomerID = UrlParameter.Optional } // Parameter defaults
);
并希望创建使用第二条路线的链接。
我该怎么做?
答案 0 :(得分:3)
如果您想专门使用路线,可以使用Html助手Html.RouteLink
:
<%= Html.RouteLink("my link", "Default2", new {OrderId=1, CustomerId=2}) %>
此外,您可以将第二条路线放在第一位:最通用的路线应该在最后,以便仅在没有找到特定路线时使用。
答案 1 :(得分:0)
订单对于MapRoutes非常重要。尝试颠倒这两个陈述的顺序。
答案 2 :(得分:0)
同样,您应该使用不同顺序的路线定义。 订单应该更具体,首先是更少
同样在您的操作链接中我会添加路由参数
答案 3 :(得分:0)
实际上不是OP要求的100%,但我需要的是一个强有力地使用特定路线的ActionResult。 RedirectToAction
总是使用错误的路线。我在RedirectToRoute("RouteName", new { action="Index" })
找到了它。