# Where Is the EndpointRoutingMiddleware Implementation in ASP.NET Core?

> Discover the EndpointRoutingMiddleware implementation in ASP.NET Core at src/Http/Routing/src/EndpointRoutingMiddleware.cs. Learn how it matches routes and selects endpoints.

- Repository: [.NET Platform/aspnetcore](https://github.com/dotnet/aspnetcore)
- Tags: internals
- Published: 2026-07-13

---

**The `EndpointRoutingMiddleware` implementation resides in [`src/Http/Routing/src/EndpointRoutingMiddleware.cs`](https://github.com/dotnet/aspnetcore/blob/main/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`](https://github.com/dotnet/aspnetcore/blob/main/src/Http/Routing/src/EndpointRoutingMiddleware.cs)** – Contains the complete middleware logic including the `Matcher` construction and endpoint selection algorithms.

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:

- **[`src/Http/Routing/src/EndpointMiddleware.cs`](https://github.com/dotnet/aspnetcore/blob/main/src/Http/Routing/src/EndpointMiddleware.cs)** – Executes the chosen endpoint's request delegate after `EndpointRoutingMiddleware` has selected it.
- **[`src/Http/Routing/src/Builder/EndpointRoutingApplicationBuilderExtensions.cs`](https://github.com/dotnet/aspnetcore/blob/main/src/Http/Routing/src/Builder/EndpointRoutingApplicationBuilderExtensions.cs)** – Provides the `UseRouting()` and `UseEndpoints()` extension methods that wire the middleware into the `IApplicationBuilder` pipeline.
- **[`src/Http/Routing/src/Matching/Matcher.cs`](https://github.com/dotnet/aspnetcore/blob/main/src/Http/Routing/src/Matching/Matcher.cs)** – Implements the actual route-matching algorithm consumed by `EndpointRoutingMiddleware`.
- **[`src/Http/Routing/test/UnitTests/EndpointRoutingMiddlewareTest.cs`](https://github.com/dotnet/aspnetcore/blob/main/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:

```csharp
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:

```csharp
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`](https://github.com/dotnet/aspnetcore/blob/main/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`](https://github.com/dotnet/aspnetcore/blob/main/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.