Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Missing routing parameter

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?

like image 852
Sergey Metlov Avatar asked Apr 26 '26 23:04

Sergey Metlov


1 Answers

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.

like image 157
Gabriele Petrioli Avatar answered Apr 29 '26 21:04

Gabriele Petrioli