Reputation: 23383
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?
Upvotes: 3
Views: 1489
Reputation: 23383
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
Upvotes: 2
Reputation: 1038850
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" })
Upvotes: 2