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
}
1
  • Check out this question. The RouteInfo class provides route information based on an url. Commented Oct 5, 2013 at 15:19

1 Answer 1

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;
    // ...
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.