Where Is the EndpointRoutingMiddleware Implementation in ASP.NET Core?
The EndpointRoutingMiddleware implementation resides in src/Http/Routing/src/EndpointRoutingMiddleware.cs within the dotnet/aspnetcore repository, where it constructs the route matcher, selects endpoints for incoming requests, and stores them in HttpContext.Features for downstream execution.
The dotnet/aspnetcore repository powers ASP.NET Core's entire routing infrastructure. Understanding the EndpointRoutingMiddleware source code reveals how the framework matches HTTP requests to endpoints before executing them.
Core Implementation Location
Primary Source File
The definitive implementation of EndpointRoutingMiddleware is located at:
src/Http/Routing/src/EndpointRoutingMiddleware.cs– Contains the complete middleware logic including theMatcherconstruction and endpoint selection algorithms.
According to the source code in this file, the middleware performs three critical functions during each request:
- Constructs the Matcher – Builds the
Matcherobject fromIEndpointRouteBuilderconfiguration to efficiently evaluate routes against incoming requests. - Selects the Endpoint – Chooses the best
Endpointbased on HTTP method, path, host, and any custom metadata attached to the route. - Stores the Selection – Persists the selected endpoint in
HttpContext.Features.Get<IEndpointFeature>()so that subsequent middleware can access the selection before execution.
How EndpointRoutingMiddleware Fits Into the Request Pipeline
Understanding the placement of EndpointRoutingMiddleware requires examining the standard ASP.NET Core request pipeline. The middleware operates in a specific sequence:
UseRouting– RegistersEndpointRoutingMiddlewareto perform route matching and endpoint selection.- Intermediate Middleware – Authentication, CORS, authorization, and other components that may inspect or modify endpoint metadata via
context.GetEndpoint(). UseEndpoints– RegistersEndpointMiddleware, which executes the request delegate of the endpoint previously selected by the routing middleware.
This separation allows middleware positioned between routing and endpoint execution to make authorization decisions or modify behavior based on the selected endpoint before any handler runs.
Supporting Files and Components
The routing system relies on several related files within the dotnet/aspnetcore repository:
src/Http/Routing/src/EndpointMiddleware.cs– Executes the chosen endpoint's request delegate afterEndpointRoutingMiddlewarehas selected it.src/Http/Routing/src/Builder/EndpointRoutingApplicationBuilderExtensions.cs– Provides theUseRouting()andUseEndpoints()extension methods that wire the middleware into theIApplicationBuilderpipeline.src/Http/Routing/src/Matching/Matcher.cs– Implements the actual route-matching algorithm consumed byEndpointRoutingMiddleware.src/Http/Routing/test/UnitTests/EndpointRoutingMiddlewareTest.cs– Contains unit tests demonstrating expected middleware behavior and edge cases.
Practical Implementation Examples
Registering the Middleware in Your Application
To enable endpoint routing in a typical ASP.NET Core application, configure the pipeline as follows:
var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
// 1️⃣ Add routing – this inserts EndpointRoutingMiddleware
app.UseRouting();
// 2️⃣ Add other middleware that may inspect endpoint metadata
app.UseAuthentication();
app.UseAuthorization();
// 3️⃣ Add endpoint execution – this inserts EndpointMiddleware
app.UseEndpoints(endpoints =>
{
endpoints.MapGet("/", async context =>
{
await context.Response.WriteAsync("Hello from endpoint routing!");
});
});
app.Run();
Accessing the Selected Endpoint in Custom Middleware
Downstream components can retrieve the endpoint selected by EndpointRoutingMiddleware using the GetEndpoint() extension method:
app.Use(async (context, next) =>
{
var endpoint = context.GetEndpoint();
if (endpoint != null)
{
// You can read metadata attached to the endpoint here
var displayName = endpoint.DisplayName;
// …
}
await next();
});
Summary
- The
EndpointRoutingMiddlewareimplementation resides insrc/Http/Routing/src/EndpointRoutingMiddleware.cswithin the dotnet/aspnetcore repository. - It constructs the
Matcher, selects endpoints based on HTTP context properties, and stores results inHttpContext.Features. - The middleware is registered via
UseRouting()and must precedeUseEndpoints()in the pipeline. - Supporting components include
EndpointMiddleware,Matcher, and the extension methods inEndpointRoutingApplicationBuilderExtensions.
Frequently Asked Questions
What is the difference between EndpointRoutingMiddleware and EndpointMiddleware?
EndpointRoutingMiddleware selects which endpoint should handle the request by matching the URL and HTTP method against configured routes, while EndpointMiddleware actually executes the selected endpoint's request delegate. The routing middleware runs during UseRouting(), whereas the execution middleware runs during UseEndpoints().
How does EndpointRoutingMiddleware store the selected endpoint?
According to the dotnet/aspnetcore source code, the middleware stores the selected endpoint in HttpContext.Features using the IEndpointFeature interface. Downstream middleware can retrieve this using context.GetEndpoint() or directly via context.Features.Get<IEndpointFeature>().
Where can I find the route matching logic used by EndpointRoutingMiddleware?
The actual route matching algorithm is implemented in src/Http/Routing/src/Matching/Matcher.cs. The EndpointRoutingMiddleware builds and utilizes this Matcher instance to evaluate incoming requests against the endpoint route table configured in your application.
Can I inspect endpoints in middleware that runs between UseRouting and UseEndpoints?
Yes. Middleware placed between UseRouting() and UseEndpoints() can access the selected endpoint via context.GetEndpoint(). This allows middleware to make authorization decisions or modify behavior based on endpoint metadata before the endpoint delegate executes.
Have a question about this repo?
These articles cover the highlights, but your codebase questions are specific. Give your agent direct access to the source. Share this with your agent to get started:
curl -s "https://instagit.com/install.md" Maintain an open-source project? Get it listed too →