Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
davidfowl opened this issue Aug 12, 2021 · 1 comment · Fixed by #35336
Closed

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

davidfowl opened this issue Aug 12, 2021 · 1 comment · Fixed by #35336
Assignees
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-minimal-actions Controller-like actions for endpoint routing old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels
Milestone

Comments

@davidfowl
Copy link
Member

davidfowl commented Aug 12, 2021

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.

@davidfowl davidfowl added feature-minimal-actions Controller-like actions for endpoint routing old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels labels Aug 12, 2021
@davidfowl davidfowl added this to the 6.0-rc1 milestone Aug 12, 2021
@davidfowl
Copy link
Member Author

We plan to treat the WebApplication as the global router for the entire pipeline so UseRouting will always use the same set of endpoints and will not create a new DefaultEndpointRouteBuilder per call.

@ghost ghost locked as resolved and limited conversation to collaborators Sep 16, 2021
@amcasey amcasey added the area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc label Jun 2, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
area-minimal Includes minimal APIs, endpoint filters, parameter binding, request delegate generator etc feature-minimal-actions Controller-like actions for endpoint routing old-area-web-frameworks-do-not-use *DEPRECATED* This label is deprecated in favor of the area-mvc and area-minimal labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants