4

I have the following route

routes.MapRoute(
    "GigDayListings",                                   // Route name
    "gig/list/{year}/{month}/{day}",                    // URL with parameters
    new { controller = "Gig", action = "List" },
    new
    {
        year = @"^[0-9]+$",
        month = @"^[0-9]+$",
        day = @"^[0-9]+$"
    }  // Parameter defaults
);

When I visit the URL

gig/list/2009/01/01

This route matches perfectly and my action is called.

Inside my view I have a helper which does the following:

var urlHelper = new UrlHelper(ViewContext);
string url = urlHelper.RouteUrl(ViewContext.RouteData.Values);

The string generated is:

http://localhost:3539/gig/list?year=2005&month=01&day=01

Why is it not

http://localhost:3539/gig/list/2005/01/01

What am I doing wrong?

tereško
  • 58,060
  • 25
  • 98
  • 150
iasksillyquestions
  • 5,558
  • 12
  • 53
  • 75

2 Answers2

9

I think your problem is that you didn't specify the route name in your call. Try to use

UrlHelper.RouteUrl(**"GigDayListings"**, ViewContext.RouteData.Values); 

overload with route name.

Cheers!

Allen Rice
  • 19,068
  • 14
  • 83
  • 115
dimarzionist
  • 18,517
  • 4
  • 22
  • 23
0

Have you checked that when you supply gig/list/2008/01/01 that it is actually using the GigDayListings route? Maybe it's using a different one

Jero
  • 159
  • 3