3

I have a MVC 2 that I migrated to MVC 3. After migrating, none of my ActionLinks worked anymore. I found it was because of my default route.

routes.MapRoute( "Default", "{controller}/{action}/{id}/{title}", new { controller = "Home", action = "Index", id = UrlParameter.Optional, title = UrlParameter.Optional } );

If I change the default route to MVCs default route, it works fine again.

routes.MapRoute( "Default", "{controller}/{action}/{id}", new { controller = "Home", action = "Index", id = UrlParameter.Optional } );

Why does having the title optional parameter break my ActionLinks?

Josh Close
  • 22,935
  • 13
  • 92
  • 140

2 Answers2

2

It's not the title parameter being optional that is problematic. In your case it is the id parameter being optional. Only the last parameter of a route definition can be optional and this rule has been enforced in ASP.NET MVC 3. Here's a similar question on this topic.

So if you want to have such route make sure that you always specify a value for the id parameter when generating those links:

@Html.ActionLink("text", "Index", new { id = "123" })
Community
  • 1
  • 1
Darin Dimitrov
  • 1,023,142
  • 271
  • 3,287
  • 2,928
2

Turns out it's a bug in the framework. http://haacked.com/archive/2011/02/20/routing-regression-with-two-consecutive-optional-url-parameters.aspx

Josh Close
  • 22,935
  • 13
  • 92
  • 140