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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With