2

As we know, a route is mapped in Global.asax file, like:

public static void RegisterRoutes(RouteCollection routes)
{
    routes.IgnoreRoute("{resource}.axd/{*pathInfo}");

    routes.MapRoute(
        name: "Default",
        url: "{controller}/{action}/{id}",
        defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
    );
}

There is a method/class where I can access route url properties name by providing a route name ?

For example, for Default, I want to call something like

public object[] GetRoutePropertiesByName(string name) {
    // process here the `controller`, `action`, `id` // there might be also other values
}
Snake Eyes
  • 16,287
  • 34
  • 113
  • 221
  • Check out [this question](http://stackoverflow.com/questions/4748342/how-to-determine-if-an-arbitrary-url-matches-a-defined-route/4749840). The `RouteInfo` class provides route information based on an url. – Henk Mollema Oct 05 '13 at 15:19

1 Answers1

2

This is how you can get a route by name:

RouteTable.Routes[routeName]

From there you can get some of the route properties:

var route = RouteTable.Routes[routeName] as Route;
if (route != null)
{
    var url = route.Url;        
    var controller = route.Defaults["controller"] as string;
    var action = route.Defaults["action"] as string;
    // ...
}
Xavier Poinas
  • 19,377
  • 14
  • 63
  • 95