I have several routes defined in my Global.asax;
When I\'m on a page I need to figure out what is the route name of the current route, because route name drives my site
RouteCollection maintains a private dictionary of named routes.
Route names can be coaxed out of it by
The extension method below follows this process:
public static string Name(this RouteBase original)
{
var routes = System.Web.Routing.RouteTable.Routes;
if (routes.Contains(original))
{
var namedMapField = routes.GetType().GetField("_namedMap", BindingFlags.NonPublic | BindingFlags.Instance);
var namedMap = namedMapField.GetValue(routes) as Dictionary;
var query =
from pair in namedMap
where pair.Value == original
select pair.Key;
return query.Single();
}
return string.Empty;
}