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:

According to the source code in this file, the middleware performs three critical functions during each request:

  1. Constructs the Matcher – Builds the Matcher object from IEndpointRouteBuilder configuration to efficiently evaluate routes against incoming requests.
  2. Selects the Endpoint – Chooses the best Endpoint based on HTTP method, path, host, and any custom metadata attached to the route.
  3. 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:

  1. UseRouting – Registers EndpointRoutingMiddleware to perform route matching and endpoint selection.
  2. Intermediate Middleware – Authentication, CORS, authorization, and other components that may inspect or modify endpoint metadata via context.GetEndpoint().
  3. UseEndpoints – Registers EndpointMiddleware, 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:

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 EndpointRoutingMiddleware implementation resides in src/Http/Routing/src/EndpointRoutingMiddleware.cs within the dotnet/aspnetcore repository.
  • It constructs the Matcher, selects endpoints based on HTTP context properties, and stores results in HttpContext.Features.
  • The middleware is registered via UseRouting() and must precede UseEndpoints() in the pipeline.
  • Supporting components include EndpointMiddleware, Matcher, and the extension methods in EndpointRoutingApplicationBuilderExtensions.

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:

Share the following with your agent to get started:
curl -s "https://instagit.com/install.md"

Works with
Claude Codex Cursor VS Code OpenClaw Any MCP Client

Maintain an open-source project? Get it listed too →