This question is related to ASP.NET Core Routing. I am doing the hands-on implementation (ASP.NET CORE 3.1 LTS) of Concept Multiple Conventional Routes. MS documentation MultipleConventional Routes
According to documentation, conventional routing is order-dependent. what that means is there are consequences for not ordering my routes correctly.
here is the code snippet of the app.UseEndpoint method with a list of configured routes.
app.UseEndpoints(endpoints =>
{
endpoints.MapControllerRoute(
name: "default",
pattern: "{controller}/{action}/{id?}");
endpoints.MapControllerRoute(
name: "CustomerHomepage",
defaults:new { controller = "Customer", action = "Index" },
pattern: "customer/{name}/index"); });
For this request https://localhost:44341/customer/sean/details
At first look at the set of route templates and order especially the first route it is a perfect match with
controller name = customer
action name = sean
id = details.
what I have in the project.
- I do have a controller name Customer but no action name as sean instead I have action name as details inside the Customer Controller.
Question The point I am trying to make is this path customer/sean/details overall should be invalid and should not navigate anywhere based on the order of the routing template. Instead, it does navigate to the action method Details in the customer controller. The question is why it is working instead it should not be based on the concept that conventional routing is order-dependent and this request URL customer/sean/details match to the first route. Also, what would be the best example for this claim that conventional routing is order-dependent.
The code for the Customer Controller is listed down
public class CustomerController: Controller
{
public IActionResult Index(string name)
{
return View();
}
public IActionResult Details(string name) {
ViewBag.CustomerName = name;
return View();
}
}