-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Add ability to modify UseRouting/UseEndpoint behavior for WebApplicationBuilder #35336
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
Conversation
We also need to avoid copying the |
src/Http/Routing/src/Builder/EndpointRoutingApplicationBuilderExtensions.cs
Outdated
Show resolved
Hide resolved
src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebApplicationTests.cs
Outdated
Show resolved
Hide resolved
src/Http/Routing/src/Builder/EndpointRoutingApplicationBuilderExtensions.cs
Outdated
Show resolved
Hide resolved
// REVIEW: this makes startup filter with userouting/useendpoints fail unless we don't remove the EndpointRouteBuilder in UseEndpoints if a global one exists | ||
// or if we stored the property in this method at the beginning and replaced it at the end. | ||
app.Properties.Remove(WebApplication.GlobalEndpointRouteBuilderKey); | ||
_builtApplication.Properties.Remove(WebApplication.GlobalEndpointRouteBuilderKey); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we should leave this (possibly).
_builtApplication.Properties.Remove(WebApplication.GlobalEndpointRouteBuilderKey); |
src/Http/Routing/src/Builder/EndpointRoutingApplicationBuilderExtensions.cs
Outdated
Show resolved
Hide resolved
After playing with this, here's another scenario that's broken that we need to preserve. The scenario here is that I have a static file called var builder = WebApplication.CreateBuilder(args);
var app = builder.Build();
app.UseStaticFiles();
app.UseRouting();
app.MapGet("/foo.txt", () => "This is a route");
app.Run(); After this change, we'll get |
That's a good catch we didn't thought of. There are two questions:
|
I like this, but in .NET 7.
No I think we should do what we did before. Only add the implicit UseRouting if it's not defined by the application. This means we need a way to detect if it's been called. |
Can we do that? (Is that something we get to do with the "double pipeline" trick? |
I think we'd just update the call to UseRouting to set another property in the app builder so we can detect it. This is what we were doing previously. |
src/Http/Routing/src/Builder/EndpointRoutingApplicationBuilderExtensions.cs
Outdated
Show resolved
Hide resolved
src/Http/Routing/src/Builder/EndpointRoutingApplicationBuilderExtensions.cs
Outdated
Show resolved
Hide resolved
src/Http/Routing/src/Builder/EndpointRoutingApplicationBuilderExtensions.cs
Outdated
Show resolved
Hide resolved
src/DefaultBuilder/test/Microsoft.AspNetCore.Tests/WebApplicationTests.cs
Show resolved
Hide resolved
if (context.HostingEnvironment.IsDevelopment()) | ||
{ | ||
// TODO: add test for this |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't have coverage for this? File an issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixes #35147
Fixes #35281
Pre-req for #34146
UseRouting will not replace the EndpointRouteBuilder if a "global" one exists, this allows us to set a global one that will effectively ignore users calling UseRouting a second time (or multiple times themselves) so we can have all routes on the same route builder.
UseEndpoints also honors the global builder so it wont throw in some of it's checks that were specific to the default route builder.
Lastly, we use the new global route builder concept in WebApplicationBuilder so we can safely wrap users code in UseRouting/UseEndpoints and put all routes onto a single route builder.