I recently came across an issue whereby an Asp.Net Core app was not behaving in the way I expected. In this particular case, I was getting strange errors, and began to suspect that the controller that I thought was reacting to my call, in fact, was not, and that the routing was to blame.
Having had a look around the internet, I came across some incredibly useful videos by Ryan Novak. One of the videos is linked at the end of this article, and I would encourage anyone working in web development using Microsoft technologies to watch it.
The particularly useful thing that I found in this was that, In Asp.Net Core 3.x and onwards, there is a clearly defined “Routing Zone” (Ryan’s terms – not mine). It falls here:
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) { … app.UseRouting(); // Routing Zone app.UseAuthentication(); app.UseAuthorization(); // End app.UseEndpoints(endpoints => … }
This means that middleware and services that make use of routing should sit in this zone, but also that you can intercept the routing. For example:
app.UseRouting(); // Routing Zone app.Use(next => context => { Console.WriteLine($"Found: {context.GetEndpoint()?.DisplayName}"); return next(context); }); app.UseAuthentication(); app.UseAuthorization(); // End app.UseEndpoints(endpoints =>
This little nugget will tell you which endpoint you’ve been directed to. There’s actually quite a lot more you can do here, too. Once you’ve got the endpoint, it has a wealth of information about the attributes, filters, and all sorts of information that makes working out why your app isn’t going where you expect much easier.
References
https://docs.microsoft.com/en-us/aspnet/core/mvc/views/overview?view=aspnetcore-3.1