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?

0

2 Answers 2

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" })
Sign up to request clarification or add additional context in comments.

1 Comment

Sorry, Phil Haack just posted that this is a bug.
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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.