views:

1450

answers:

2

Hey all,

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

+2  A: 

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.

Gilligan
While not an optimal solution, I do know this will work
Jim Geurts
+3  A: 

The fact that your parameters are integers has nothing to do with your problem. The route definition you want to be used isn't actually being used, which is why the generated URL is using query string parameters instead of building the structure you want.

Routes are evaluated top-down, so you likely have a more generic route definition that is satisfying your requested URL generation. Try moving the route you displayed in this post to the top of your route definitions, and you'll see that your generated link is as you'd expect. Then look into modifying your route definitions to either be more specific, or just move them around as necessary.

Debugging these types of scenarios can be a huge pain. I'd suggest downloading Phil Haack's route debugger, it will make your life a lot easier.

LostInTangent
The route is at the top, unforutnately. I'll take a look at the debugger, though.
Jim Geurts