tags:

views:

57

answers:

2

Can anyone explain why this code with the given routes returns the first route?

routes.MapRoute(null, "user/approve", new { controller = "Users", action = "Approve" }),
routes.MapRoute(null, "user/{username}", new { controller = "Users", action = "Profile" }),
routes.MapRoute(null, "user/{username}/{action}", new { controller = "Users" }),
routes.MapRoute(null, "user/{username}/{action}/{id}", new { controller = "Users" }),
routes.MapRoute(null, "search/{query}", new { controller = "Artists", action = "Search", page = 1 }),
routes.MapRoute(null, "search/{query}/{page}", new { controller = "Artists", action = "Search" }),
routes.MapRoute(null, "music", new { controller = "Artists", action = "Index", page = 1 }),
routes.MapRoute(null, "music/page/{page}", new { controller = "Artists", action = "Index" })

var pageLinkValueDictionary = new RouteValueDictionary(this.linkWithoutPageValuesDictionary);
pageLinkValueDictionary.Add("page", pageNumber);
var virtualPathData = RouteTable.Routes.GetVirtualPath(this.viewContext.RequestContext, pageLinkValueDictionary);

Here GetVirtualPath always returns user/approve, although there is no {page} parameter in the route. Furthermore, everything works as expected without the first route.

I have found this link http://www.codeplex.com/aspnet/WorkItem/View.aspx?WorkItemId=2033 but it wasn't very helpful. It looks like GetVirtualPath was not implemented with large collections of routes in mind.

I am using ASP.Net MVC 1.0.

Update. I can rearrange the routes and get the appropriate results. But I would really like to understand why on Earth does it choose the user/approve route at all.

+1  A: 

Try adding action and controller in the RouteValueDictionary

šljaker
Can't. My method is action and controller agnostic, hence, the context.
HeavyWave
Funny, I was looking at it yesterday and did exactly that.
HeavyWave
A: 

Use this link to dig into why it works the way it does http://www.codeplex.com/aspnet/WorkItem/View.aspx?WorkItemId=2033.

Rearranging the routes should help in most cases, although it can be a "smoke and mirrors" approach at times.

HeavyWave