G-Wiz
G-Wiz

Reputation: 7426

ASP.NET Routing using wrong route to generate URL; route values become query string parameters

Relevant route registration code:

routes.MapRoute(
  "QuestionsMostRecent",  
  "questions", 
  new { controller = "questions", action = "most_recent" }
);
routes.MapRoute(
  "ControllerActionFormat", 
  "{controller}/{action}.{format}"
);

Route generation code:

Url.RouteUrl(new {
  controller = "questions", 
  action = "most_recent", 
  format = "rss" 
});

I expect to receive "/questions/most_recent.rss", but instead I receive "/questions?format=rss". I realize I can force my expected result by referencing the route name "ControllerActionFormat", but I am curious about why exactly the routing system is matching the first route. Can anyone shed some light on this?

Upvotes: 1

Views: 440

Answers (1)

womp
womp

Reputation: 116977

Because both of them match, but you have the more broadly defined route registered first. Register the more specific route first and it will solve the issue.

Upvotes: 1

Related Questions