Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/asp.net-mvc/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net mvc 路由配置与基于属性的路由mvc,优先级更高_Asp.net Mvc_Model View Controller_Routing - Fatal编程技术网

Asp.net mvc 路由配置与基于属性的路由mvc,优先级更高

Asp.net mvc 路由配置与基于属性的路由mvc,优先级更高,asp.net-mvc,model-view-controller,routing,Asp.net Mvc,Model View Controller,Routing,有人问我,假设我在route.config中为URL定义了路由,并且在基于属性的路由中定义了相同的路由。那么在每种情况下谁的优先级更高。如果我们可以在route.config中实现同样的功能,那么基于属性的路由又有什么用呢 那么在每种情况下谁的优先级更高 这将取决于您是在常规路由之前还是之后调用routes.mapmvcattributterroutes()扩展方法。例如: public static void RegisterRoutes(RouteCollection routes) {

有人问我,假设我在route.config中为URL定义了路由,并且在基于属性的路由中定义了相同的路由。那么在每种情况下谁的优先级更高。如果我们可以在route.config中实现同样的功能,那么基于属性的路由又有什么用呢

那么在每种情况下谁的优先级更高

这将取决于您是在常规路由之前还是之后调用
routes.mapmvcattributterroutes()
扩展方法。例如:

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 }
    );
}
在这种情况下,基于属性的路由将首先添加到路由表中,并优先

如果我们能够实现 在route.config中也是如此

属性路由为您提供了更大的灵活性,并将路由放置在实际使用它们的操作旁边。但这确实是一个偏好的问题。与传统方法相比,Microsoft添加了基于属性的路由,以便在应用程序中有一种替代方法来定义路由

那么在每种情况下谁的优先级更高

这将取决于您是在常规路由之前还是之后调用
routes.mapmvcattributterroutes()
扩展方法。例如:

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 }
    );
}
在这种情况下,基于属性的路由将首先添加到路由表中,并优先

如果我们能够实现 在route.config中也是如此

属性路由为您提供了更大的灵活性,并将路由放置在实际使用它们的操作旁边。但这确实是一个偏好的问题。与传统方法相比,Microsoft添加了基于属性的路由,以便在应用程序中有一种替代方法来定义路由