如何强制链接使用特定路由(MVC路由)
我有这两条路线:
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
);
并且想要创建使用第二条路线的链接。
我该怎么做?
I have these two routes:
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
);
and want to create link that uses the second route.
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果你想专门使用一个路由,你可以使用 Html 帮助器
Html.RouteLink
:另外,你可以把第二个路由放在前面:最通用的路由应该放在最后,以便仅当未找到特定路线时才使用。
If you want to specifically use a route, you can use the Html helper
Html.RouteLink
:Also, you can put the second route first : the most generic route should be at the end, in order to be used only when no specific route was found.
对于 MapRoutes 来说,顺序非常重要。尝试颠倒这两个语句的顺序。
Order is very important w/ MapRoutes. Try reversing the order of those two statements.
您还应该以不同的顺序使用路由定义。
在您的操作链接中,顺序应该首先更具体,然后
再更具体,我将添加路由参数
As well you should use definition of routes in different order.
the order should be more specific first to less
also in your action links i would add routing parameters
实际上并不是 100% OP 要求的,但我需要的是一个强制使用特定路线的 ActionResult。
RedirectToAction
总是使用错误的路线。我在RedirectToRoute("RouteName", new { action="Index" })
中找到了它。Actually not 100% what the OP asked for, but what I needed was an ActionResult that forcefully uses a specific route.
RedirectToAction
always used the wrong route. I found it inRedirectToRoute("RouteName", new { action="Index" })
.