views:

31

answers:

1

How do I find the Route Name that is being used when inside of the ControllerBase using it?

For example, I have a route like this:

routes.MapRoute("Search", "{controller}.mvc/{action}/{criterion}", new { controller = "", action = "", criterion = "" });

...and in my ControllerBase I need the Object.Property that contains "Search" or the name of the Route that the ControllerBase is currently using if not search.

Thank you.

-Jessy Houle

+2  A: 

This question has come up a number of times in various forms. The short answer is that you can't - the Route name is not stored anywhere in the routing data.

There have been various workarounds proposed for this. See this answer for some code for one implementation of Route name tracking. There are others around as well.

womp
Is there any way that you can think of to re-create the URL with one changed value such as this:UrlHelper u = new UrlHelper(Url.RequestContext, Url.RouteCollection);u.RequestContext.RouteData.Values["borrowerId"] = -999;// Recreate the exact same URL with the changed borrowerIdI cannot use string replace, as some of the other values on the URL may be the same as the borrowerId.
Jessy Houle
Use DataTokens on a Route to supply extra "metadata" that only applies to your code. Routing will ignore data tokens, but will pass it along so your code can interpret it.
Haacked