具有属性路由的 MVC5:如何获取与特定操作关联的路由数据

MVC5 with attribute routing: how to get route data associated with particular action

假设我有一个 ActionDescriptor(或 MethodInfo)对象指向我的应用程序中的某个操作方法。我想获取与此操作关联的路由 table 的条目(System.Web.Routing.Route 个对象)。

是否有某种框架方法可以为我提供这些信息,或者我是否必须以某种方式解析路由 table?在第二种情况下,您建议如何执行此操作?

我就是这样做的:

var routeProvider = new DefaultDirectRouteProvider();
var routeEntries = routeProvider.GetDirectRoutes(
    _actionDescriptor.ControllerDescriptor, new[] { _actionDescriptor }, new DefaultInlineConstraintResolver());