Search code examples
asp.net-mvc-5asp.net-mvc-routing

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


Let's assume I have an ActionDescriptor (or MethodInfo) object that points to some action method in my application. I want to get route table's entries (System.Web.Routing.Route objects) associated with this action.

Is there, by any chance, some framework method that might get me this information, or do I have to parse the route table somehow? How would you suggest to do this, in the second case?


Solution

  • That's how I did this:

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