【问题标题】:Route Config Vs Attribute based routing mvc whose priority is moreRoute Config Vs Attribute based routing mvc,优先级更高
【发布时间】:2016-02-12 06:25:44
【问题描述】:

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

【问题讨论】:

    标签: asp.net-mvc model-view-controller routing


    【解决方案1】:

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

    这取决于您是在常规路由之前还是之后调用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 }
        );
    }
    

    在这种情况下,基于属性的路由将首先添加到路由表中并优先。

    如果我们可以实现基于属性的路由有什么用呢? 在 route.config 中相同。

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

    【讨论】:

      猜你喜欢
      • 2016-04-10
      • 2018-12-02
      • 2016-10-24
      • 2015-09-28
      • 1970-01-01
      • 2017-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多