Below is some simple code
public class Startup {
public void ConfigureServices(IServiceCollection services) {
}
public void Configure(IApplicationBuilder app, IWebHostEnvironment env) {
app.UseDeveloperExceptionPage();
app.UseRouting();
app.Use(async (context, next) => {
var endpoint = context.GetEndpoint(); // <--------I put a debugger here
await next();
});
app.UseEndpoints(endpoints => {
endpoints.MapGet("routing", async context => {
await context.Response.WriteAsync("Request Was Routed");
});
});
app.Use(async (context, next) => {
await context.Response.WriteAsync("Terminal Middleware Reached");
});
}
}
When the breakpoint is hit, I can see some properties has been added to the Endpoint instance, for example, RoutePattern property

But Endpoint doesn't have a RoutePattern property:
public class Endpoint {
public Endpoint(RequestDelegate requestDelegate, EndpointMetadataCollection metadata, string displayName);
public string DisplayName { get; }
public EndpointMetadataCollection Metadata { get; }
public RequestDelegate RequestDelegate { get; }
public override string ToString();
Then how does RoutePattern property is added to the Endpoint instance?
Another question is, when I run the application, the breakpoint always get hit twice, not once, why is that, because there is only one request, the breakpoint should only hit once?
CodePudding user response:
Endpoint class really doesn't have RoutePattern property, but it's child class RouteEndpoint has this property (Microsoft.AspNetCore.Routing.RouteEndpoint), when you watch it when it's debugger, you can see it.
And why hit it twice, I'm afraid there's some other code in your app leads it, in my newly created asp.net core MVC app, when the app starts, it only hit it once, but in my signalr mvc project, it hit twice because of the connection of signalr. So I think you need to check your code.
By the way, this document about middleware execution order may help.

