Skip to content

Adding routes using UseEndpoints and WebApplication duplicates route data sources added in UseEndpoints #35147

@davidfowl

Description

@davidfowl

Describe the bug

Adding routes top level and via UseEndpoints duplicated the routes globally. This is because the WebApplicationBuilder internally calls UseEndpoints which results in the datasource being copied twice. This may have performance implications on any code that looks at EndPointDataSource to perform actions (like link generation).

To Reproduce

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

app.UseRouting();

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

app.UseEndpoints(routes =>
{
    routes.MapGet("/hi", () => "Hi World");
    routes.MapGet("/heyo", () => "Heyo World");
});

app.Start();

var ds = app.Services.GetRequiredService<EndpointDataSource>();
foreach (var e in ds.Endpoints)
{
    Console.WriteLine(e.DisplayName);
}

app.WaitForShutdown();

The following prints out:

/hi HTTP: GET
/heyo HTTP: GET
/hi HTTP: GET
/heyo HTTP: GET
/ HTTP: GET

Further technical details

  • ASP.NET Core version: Microsoft.AspNetCore.App 6.0.0-rc.1.21406.15

cc @javiercn

Metadata

Metadata

Assignees

Labels

area-minimalIncludes minimal APIs, endpoint filters, parameter binding, request delegate generator etcbugThis issue describes a behavior which is not expected - a bug.feature-minimal-hostingfeature-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