Skip to content

Routes added via WebApplication get attached to the last UseRouting call #35281

@davidfowl

Description

@davidfowl

Describe the bug

Today we add routes from the WebApplication we copy the route data sources to the last call to UseEndpoints, this results in bizzare behavior when adding multiple calls to UseRouting and UseEndpoints.

To Reproduce

var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();

app.UseRouting();

app.Use((context, next) =>
{
    Console.WriteLine("Selected endpoint is " + (context.GetEndpoint()?.DisplayName is string s ? s : "null"));
    return next(context);
});

app.MapGet("/", () => "Hello World");

app.UseEndpoints(endpoints =>
{
    endpoints.MapGet("/hi", () => "Hello Endpoints");
});

app.UseRouting();
app.UseEndpoints(_ => { });

app.Run();

If you request / you'll see "Selected endpoints is null". It's because the middleware runs after the first UseRouting middleware but the route is matched in the second UseRouting middleware.

Metadata

Metadata

Assignees

Labels

area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcfeature-minimal-actionsController-like actions for endpoint routingold-area-web-frameworks-do-not-use*DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions