How do I get Route name from RouteData?

Andrey picture Andrey · Oct 17, 2010 · Viewed 30.6k times · Source

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 menu.

How can this be done?

Answer

Haacked picture Haacked · Oct 23, 2010

Unfortunately, it's not possible to get the route name of the route because the name is not a property of the Route. When adding routes to the RouteTable, the name is used as an internal index for the route and it's never exposed.

There's one way to do this.

When you register a route, set a DataToken on the route with the route name and use that to filter routes.

The easiest way to do #1 is to probably write your own extension methods for mapping routes.