Josh Close
Josh Close

Reputation: 23383

ASP.NET MVC 3 Route Breaks ActionLink Functionality

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

Answers (2)

Darin Dimitrov
Darin Dimitrov

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

Related Questions