Fairly straightforward issue.
<a asp-controller="Guide" asp-action="Index" asp-route-title="@Model.Title">Read more</a>
produces the link /Guide?title=Guide_no_1 but it is supposed to produce the link /Guide/Guide_no_1/
The documentation i can find all specify that its supposed to output /Guide/Guide_no_1/ so I have the feeling there is something i missed, a setting or some attribute somewhere.
the routes i have are like this, just incase they have an impact on link creation
[Route("/Guide/")]
public IActionResult Index() { ... }
[Route("/Guide/{title}/{page?}/")]
public IActionResult Index(string title, int page = 0) { ... }
[Route("/Guide/OnePage/{title}/")]
public IActionResult Index(string title) { ... }
You need to specify the ordering of your attribute routes.
You need to order your routes so you evaluate first the most specific routes. In the case of attribute routing, you will do that using the Order parameter:
[Route("Guide/OnePage/{title}/", Order = 1)]
public IActionResult Index(string title) { ... }
[Route("Guide/{title}/{page?}/", Order = 2)]
public IActionResult Index(string title, int page = 0) { ... }
[Route("Guide/", Order = 3)]
public IActionResult Index() { ... }
Now you will notice that you also have a problem with the 2 routes with parameters Guide/OnePage/{title}/ and Guide/{title}/{page?}/. Since they have the same controller, action and required parameters, there is no way MVC can differentiate them and the one ordered first will always win!
Or assign a specific route name to the one that will always lose, so you can create links using that name:
[Route("Guide/{title}/{page?}/", Name = "titleAndPage", Order = 2)]
public IActionResult Index(string title, int page = 0) { ... }
<a asp-route="titleAndPage" asp-route-title="fooTitle">Read more</a>
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