3

I registered a route:

routes.MapRoute(
    "Journals",
    "Journals/{year}/{month}/{id}",
    new {
        controller = "Journals",
        action = "Get",
        year = UrlParameter.Optional,
        month = UrlParameter.Optional,
        id = UrlParameter.Optional
    }
);

Action:

public ActionResult Get(int? year, int? month, int? id)

Later in view (just to check):

@Url.Action("Get", "Journals")
@Url.Action("Get", "Journals", new { year = 2013 })
@Url.Action("Get", "Journals", new { year = 2013, month = 4 })
@Url.Action("Get", "Journals", new { year = 2013, month = 4, id = 1 })

And result is:

/Journals
/Journals
/Journals/2013/4
/Journals/2013/4/1

So the 2nd URL missed the parameter. What's wrong?

Sergey Metlov
  • 25,747
  • 28
  • 93
  • 153
  • 1
    A useful tool for these situations is Phil Haack's route debugger: http://haacked.com/archive/2008/03/13/url-routing-debugger.aspx – neontapir May 30 '13 at 21:09

1 Answers1

1

You cannot have more than 1 continuous optional route parameters.. as it cannot understand which one is missing..

the 2013 in /Journals/2013 could be interpreted as either a year or a month or an id

See Infinite URL Parameters for ASP.NET MVC Route for a workaround using a catch-all route parameter.

Community
  • 1
  • 1
Gabriele Petrioli
  • 191,379
  • 34
  • 261
  • 317