Thoughts from Dan Miser RSS 2.0
# Sunday, January 15, 2012
What's the difference between these 2 methods?

Html.ActionLink(string linkText, string actionName, object routeValues, object htmlAttributes)
Html.ActionLink(string linkText, string actionName, RouteValueDictionary routeValues, IDictionary htmlAttributes)

Yes, they have the same number of parameters. Yes, they even have the same parameter names. So in essence the intent of calling either of these methods is to build up a link with the passed in route values and HTML attributes. Yet the difference between these 2 methods couldn't be more pronounced.

Take this line of code:


@Html.ActionLink("Back to List", "Index", TempData["SavedRouteValues"], null)
What I wanted was an easy way to retain the settings of a Telerik ASP.NET MVC Grid and build up a link to bring me back to the correct state. This line of code does that. However, it generates a link similar to this (word-wrapped here for clarity):

http://www.foo.com/Grid?Count=3&Keys=System.Collections.Generic.Dictionary%602%2BKeyCollection%5BSystem.String%2CSystem.Object
%5D&Values=System.Collections.Generic.Dictionary%602%2BValueCollection%5BSystem.String%2CSystem.Object%5D

Brutal. Ugly.

To fix this, just use this code (a simple typecast) to force the correct overloaded method to be picked:


@Html.ActionLink("Back to List", "Index", (RouteValueDictionary)TempData["SavedRouteValues"], null)

The moral of the story? If you're an API desiger, don't make overloaded methods lightly. Make sure they add value and distinction. A strong type vs. an object reference doesn't pass that test. If you do violate that rule, however, then at least be sure that the overload fulfills the intent and implied contract of that method.

Sunday, January 15, 2012 3:52:59 PM (GMT Standard Time, UTC+00:00)  #    Comments [0] -
ASP.NET MVC
Navigation
Archive
<December 2014>
SunMonTueWedThuFriSat
30123456
78910111213
14151617181920
21222324252627
28293031123
45678910
Blogroll
About the author/Disclaimer

Disclaimer
The opinions expressed herein are my own personal opinions and do not represent my employer's view in any way.

© Copyright 2014
Dan Miser
Sign In
Statistics
Total Posts: 388
This Year: 0
This Month: 0
This Week: 0
Comments: 630
Themes
Pick a theme:
All Content © 2014, Dan Miser
DasBlog theme 'Business' created by Christoph De Baene (delarou)