Asp.Net MVC: How do I get Html.ActionLink to render integer values properly?

asp.net-mvc, c#, routes

Solution

I would suggest formatting the Year, Month, and Day as Strings instead. Think about this: Will you be doing any math on these "integers"? Probably not, so there really is no point for making them integers. Once you have them as Strings you can force the leading zero format.

Problem

I have an asp.net mvc application with a route similar to: ``` routes.MapRoute("Blog", "{controller}/{action}/{year}/{month}/{day}/{friendlyName}", new { controller = "Blog", action = "Index", id = "", friendlyName="" }, new { controller = @"[^\.]*", year = @"\d{4}", month = @"\d{2}", day = @"\d{2}" } ); ``` My controller action method signature looks like: ``` public ActionResult Detail(int year, int month, int day, string friendlyName) { // Implementation... } ``` In my view, I'm doing something like: ``` <%= Html.ActionLink<BlogController>(item => item.Detail(blog.PostedOn.Year, blog.PostedOn.Month, blog.PostedOn.Day, blog.Slug), blog.Title) %> ``` While the url that is generated with ActionLink works, it uses query string variables rather than URL rewriting. For example, it would produce /blog/detail/my-slug?year=2008&month=7&day=5 instead of /blog/detail/2008/07/05/my-slug Is there a way to get the generic version of ActionLink to properly pad the integer values so that the url comes out as expected? Thanks Jim

Original source