Monday, August 16, 2010

Html.ActionLink Displaying The Word Length In The URL - What?

Don't you just love crazy weirdness when you create urls? I sure do....NOT! Sometimes the smallest things can provide you with the biggest pain - especially when you are pressed for time. I ran across a crazy little problem in ASP.NET MVC 2 with an url I created using Html.Actionlink.

I wanted an url that looked like this -

http://www.mysite.com/Project/Pages/someGuidId.

I used the following Html.Actionlink -

<%= Html.ActionLink("View Project Pages", "Pages", "Project",
new { id = page.PageId })%>


This generated an url with a Length property at the end of it -

http:/www.mysite.com/Project/Pages?Length=7

What in the world is that? Length is NOT my friend! Fear not though! This error is very easy to solve with minimal effort. Adding one value makes the difference. I just needed to change my Html.Actionlink to the following:

<%= Html.ActionLink("View Project Pages", "Pages", "Project",
new { id = page.PageId },
null)%>

That nice little null does the trick quite nicely and makes everyone happy!

Smooches,

Kila

Problem:
<%= Html.ActionLink("View Project Pages", "Pages", "Project",
new { id = page.PageId })%>

generates http:/www.mysite.com/Project/Pages?Length=7

Solution:
<%= Html.ActionLink("View Project Pages", "Pages", "Project",
new { id = page.PageId },
null)%>