1

I'm using nUnit to test the routing of my MVC2 project.

When I register my routes in Global.asax.cs, I give each route a unique name and specify its RouteData, eg:

routes.MapRoute(
    "ShowRecord",
    "{controller}/{id}",
    new { action = "Show" },
    new { id = @"^\d+$" }
);

In my unit tests I then invoke RegisterRoutes on a RouteCollection object, and inspect the resulting RouteValueDictionary against each url that I want to test. I use a mocked HttpContext for this, and everything works fine.

However, what I'd really like to know is, which named route(s) matched the supplied URL? Once my unit test has obtained the RouteData object corresponding to the URL under test, can I discover specifically which route was matched? Either by name (eg "ShowRecord" in the example above), or by its index in the RouteCollection object?

Si Phillipson
  • 73
  • 1
  • 5

1 Answers1

2

Unfortunately, you cannot do this. See How do I get Route name from RouteData? for more context.

What you could do is add the route name to the route's DataTokens dictionary. DataTokens are used to mark a route in some way that's significant to you and your application. They don't affect anything with regarding to route matching and url generation.

The purpose of the route name is to provide a lookup key to the routing engine for looking up routes. It acts much like an index in SQL Server.

Community
  • 1
  • 1
Haacked
  • 58,045
  • 14
  • 90
  • 114