I've got the following ActionMethod:
[Route("List/{listType}/{listID?}/{pageNumber?}/{pageSize?}/{output?}")]
public ActionResult List(int listType, int listID = 0, int pageNumber = 0, int pageSize = 10, string output = "html")
{
// Do Stuff
}
The first parameter is required, the rest is optional.
When I call the following default MVC method to create an ActionLink
@Html.ActionLink("MyLink", "List", "Message", new { listType = 4 }, null)
the link is generated to:
/Message/List?listType=4
I assumed it should be:
/Message/List/4
When I click the link, I receive a 404 error page, that the page is not found.
When I pass in the default value for the second parameter
@Html.ActionLink("MyLink", "List", "Message", new { listType = 4, listID = 0 }, null)
The link generated is correct:
/Message/List/4/0
However, when the value is optionally, I want to create the short link (/Message/List/4).
I've checked and double checked whether the naming of the parameter is correct, but this is not the problem...
I've also added a second List method
[Route("List/{listType})]
public ActionResult List(int listType)
{
// Do Stuff
}
The link with only 1 parameter is generated correctly, but when I pass in more parameters, they are generated like:
/Message/List/4?listID=5
Offcourse routes.MapMvcAttributeRoutes(); has been added to the RegisterRoutes function...
What problem do I not see which causes to generate an incorrect link when I only pass in 1 parameter?
I was only able to reproduce this problem with the following code:
And fix it with this code:
Routes are order sensitive and must be declared from most specific to least specific in order to work.