There is one thing to mention. If you don't use "app.useRouting()" . Asp.net core will automatically use it at the beginning of middleware pipeline. But when you manually use it, you are forcing a pipeline order to use it.
When app.useRouting execute, it will find all the possible endpoints throughout the project. Endpoint could be defined by app.MapControllerRoute() or app.MapControllers or app.Map etc. It doesn't matter this endpoint is defined before "useRouting" or after. "useRouting" find them like regarding the whole project code as text.
You could add the below code to anywhere of your pipeline.
// Location 1: before routing runs, endpoint is always null here
app.Use((context, next) =>
{
Console.WriteLine($"1. Endpoint: {context.GetEndpoint()?.DisplayName ?? "(null)"}");
return next(context);
});
app.UseRouting();
// Location 2: after routing runs, endpoint will be non-null if routing found a match
app.Use((context, next) =>
{
Console.WriteLine($"2. Endpoint: {context.GetEndpoint()?.DisplayName ?? "(null)"}");
return next(context);
});
When you visit any matching URL. You will find it always find the endpoint after you use "useRouting"
Also, as what I mentioned in the beginning, deleting "app.useRouting()" also works to find endpoint. And for most of the scenario. It makes no difference.
If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.