【发布时间】:2019-03-30 01:58:38
【问题描述】:
我假设约定路由将首先添加到路由表中,因为它正在像这样在 global.asax 文件中注册
RouteConfig.RegisterRoutes(RouteTable.Routes);
现在我在 route.config 中有这样的路由
public static void RegisterRoutes(RouteCollection routes)
{
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
routes.MapMvcAttributeRoutes();
}
我有这样的属性路由
[Route("students/{id?}")]
public ActionResult Index(int? id)
{
return View(id);
}
现在当我使用 URL 时
localhost:4200//学生
学生路线被成功调用,但是当我使用这样的路线时
localhost:4200//students/40
我收到错误,我不知道原因。当我从 RouteConfig 类中删除路由时,我可以成功调用它。
谁能解释我为什么以及如何?
【问题讨论】:
-
我认为 MapMvcAttributeRoutes 的调用必须在 MapRoute 调用之前。
-
你确定吗?根据 msdn 中的文档,它不是
-
路由按照添加到路由表的顺序被调用。第一场比赛获胜,它不做任何进一步的检查。目标属性路由通常在更通用的基于约定的路由之前添加,以避免路由冲突。
-
@LijinDurairaj 除非是拼写错误,否则没有文档会显示在基于约定的路由之后添加的属性路由。
-
感谢@Nkosi 它有效
标签: asp.net-mvc asp.net-mvc-4 asp.net-mvc-routing