Reputation: 614
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.
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();
}
}
Upvotes: 0
Views: 874
Reputation: 5413
Convention based routing in ASP.NET Core 3.x+ is not fundamentally order based. Instead, the routing system builds an acyclic graph which is used to match the incoming URL to the best route. In your example, the literal match to customer
makes the send route the best match.
In this series I describe how you can visualize all the routes in your ASP.NET Core applications, which may help you to understand how routes are combined.
Upvotes: 1
Reputation: 27793
From this doc about "Routing in ASP.NET Core", you would find the process of matching an incoming request to an endpoint, like below.
URL matching operates in a configurable set of phases. In each phase, the output is a set of matches. The set of matches can be narrowed down further by the next phase. The routing implementation does not guarantee a processing order for matching endpoints. All possible matches are processed at once. The URL matching phases occur in the following order. ASP.NET Core:
The list of endpoints is prioritized according to:
All matching endpoints are processed in each phase until the EndpointSelector is reached. The EndpointSelector
is the final phase. It chooses the highest priority endpoint from the matches as the best match. If there are other matches with the same priority as the best match, an ambiguous match exception is thrown.
Upvotes: 0