... produces ...
Notice "Length=3" in the query string? That's probably not what you expected. We see this output because we are calling the ActionLink overload where “Car” is interpreted as route data and the anonymous object we want to use for route data is instead interpreted as the HTML attributes parameter.
... it renders ...
And this is probably the output you want to see, because the third parameter went from being routing data to controller name, the fourth parameter went from being HTML attributes to route data, and the last parameter is now HTML attributes. All because we passed an empty (null) value as an additional parameter.
Kind of weird, don't you think?
But it doesn't stop with the overloads.
Html.Help!
Tell me if I'm missing something obvious...
Let's say you want to write a custom HTML helper to render an image tag with a specific purpose.
Let's also say the image tag needs to start with its src attribute set to a well known image. Sounds easy, but if you want to ensure the path is correct you might want to make a call to Url.Content - except the Url helper isn't available inside an Html helper method.
var rollerPath = "?"; // Resolve(~/Content/roller.gif) ?
You could always make FlightChart an extension method for UrlHelper, but it feels strange to produce HTML markup from a Url helper. Another option is to construct a new instance of UrlHelper inside the the Html helper.
But it seems silly to create a new UrlHelper when one is already available somewhere else inside of the request. What if we grabbed a reference to the existing UrlHelper?
Eek! That's even worse!
What if we just forget about UrlHelper and turn to our old friend VirtualPathUtility?
Better - but don't try to run any unit tests on the above code.
That helper is open to HTML injection because it accepts parameters and blindly injects them into HTMl with String.Format.
Use TagBuilder instead, it is your friend.
Thanks, @Damien, updated.
In all seriousness, go look at the equivalents in FubuMVC. *They* don't suffer from the same awkwardness as the OOTB experience in ASP.Net. With or without the Html convention support, you could use Fubu's helpers inside ASP.net MVC apps. Javier even has an MvcTurbine add in for just that.
In first case I prefer to stay as much in HTML as posible, so it's better to avoid Html.ActionLink helper at all. I prefer to write it like Detail.
In second case why not don't you promote the rollerPath to parameter?
The MVC folks just do not use their product enough. You only find such design errors if you dogfood.
I will, absolutely.
Comments are closed.
The Podcast!
