Har*_*rma 0 model-view-controller asp.net-mvc routing
有人问我,假设我已经在 route.config 中为 URL 定义了路由,并且我已经在基于属性的路由中定义了相同的路由。那么在每种情况下谁的优先级更高。如果我们可以在 route.config 中实现相同的基于属性的路由有什么用。
那么在每种情况下谁的优先级更高。
这取决于您是routes.MapMvcAttributeRoutes()在常规路由之前还是之后调用扩展方法。例如:
public static void RegisterRoutes(RouteCollection routes)
{
...
routes.MapMvcAttributeRoutes(); //Attribute routing
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
Run Code Online (Sandbox Code Playgroud)
在这种情况下,基于属性的路由将首先添加到路由表中并优先。
如果我们可以在 route.config 中实现相同的基于属性的路由有什么用。
属性路由为您提供了更大的灵活性,并将路由放置在实际使用它们的操作旁边。但这确实是一个偏好问题。与传统方法相比,Microsoft 添加了基于属性的路由,以便在应用程序中定义路由的替代方法。