ActionLink doesn't URL encode?

I've complained about this before, but I'm just going to do it again. It doesn't make any sense, not for me anyway!

When first starting out with ASP.NET MVC I was kind of expecting methods like the extension methods ActionLink and Action to make nice Urls, without any effort from me. I guess I was wrong, or maybe I've totally missed something. Let me give you a small example taken from a site I'm working on at the moment.

			context.MapRoute(
					"BSMC_News",
					"BSMC/News/{edition}/{title}/{id}",
					new { controller = "News", action = "Read", edition = "en-GB" }
				);
Url.Action("Read", "News", new { id = news.Id, edition = edition, title = news.Headline })

id is easy, it's a number (Int32), edition is a string, actually it's a CultureInfo id. Then there's title, the headline of a news item. We all know what happens when the users start using this nice new web-site, they'll put all sorts of strange character in the title, and your fine new application will fail.

So I started off by making this headline: "December Results Released.". A very good test, it failed right away! The Url that was generated was: "/News/en-GB/December Results Released./44" and you can't have a period in the middle of the Url, so no dice!

Please tell me I'm doing something wrong, and this of course should be handled automatically! I know how to fix it, but seriously, shouldn't it be fixed in the framework?

Categories: ASP.NET MVC - Routing

Posted by Steen F. Tøttrup on 12 October 2011 21:04. There are 0 comments.

Write new comment