I have the following controller:
[Route("blog")]
[Route("{locale:regex(^(de|es|fr)$)}/blog", Order = -1)]
public class BlogController : Controller {
[HttpGet("{id:int}.htm")]
[HttpGet("{slug}/{id:int}.htm")]
public IActionResult Details(string? slug, int id) {
return View();
}
}
Now if I try to generate the following URLs:
I would expect the following:
However this returns:
I have tried changing the order on all the routing attributes but I cannot get it to return the desired result and I’d appreciate any help.
The following example gives the intended results:
public class BlogController : Controller
{
[Route("{locale:regex(^(de|es|fr)$)}/blog/{slug}/{id:int}.htm")]
[Route("{locale:regex(^(de|es|fr)$)}/blog/{id:int}.htm", Order = 1)]
[Route("blog/{slug}/{id:int}.htm", Order = 2)]
[Route("blog/{id:int}.htm", Order = 3)]
public IActionResult Details(string? slug, int id)
{
return View();
}
}
This approach uses an earlier Order
for more specific routes, so that those are checked first. The obvious downside is the verbosity, but it's a working solution based on the requirements described.