-
Notifications
You must be signed in to change notification settings - Fork 10.3k
Update ExceptionHandler middleware pipeline construction to reinvoke UseRouting in error branch #34991
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
Update ExceptionHandler middleware pipeline construction to reinvoke UseRouting in error branch #34991
Changes from all commits
575aa50
d469012
596f8b7
fbd0d40
1f2d58b
1a0eea1
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -14,7 +14,7 @@ namespace Microsoft.AspNetCore.Builder | |
/// </summary> | ||
public static class EndpointRoutingApplicationBuilderExtensions | ||
{ | ||
private const string EndpointRouteBuilder = "__EndpointRouteBuilder"; | ||
internal const string EndpointRouteBuilder = "__EndpointRouteBuilder"; | ||
|
||
/// <summary> | ||
/// Adds a <see cref="EndpointRoutingMiddleware"/> middleware to the specified <see cref="IApplicationBuilder"/>. | ||
|
@@ -42,10 +42,48 @@ public static IApplicationBuilder UseRouting(this IApplicationBuilder builder) | |
throw new ArgumentNullException(nameof(builder)); | ||
} | ||
|
||
return UseRouting(builder, true); | ||
} | ||
|
||
/// <summary> | ||
/// Adds a <see cref="EndpointRoutingMiddleware"/> middleware to the specified <see cref="IApplicationBuilder"/>. | ||
/// </summary> | ||
/// <param name="builder">The <see cref="IApplicationBuilder"/> to add the middleware to.</param> | ||
/// <param name="overrideEndpointRouteBuilder">Whether a new <see cref="EndpointRouteBuilder"/> should be created.</param> | ||
/// <returns>A reference to this instance after the operation has completed.</returns> | ||
/// <remarks> | ||
/// <para> | ||
/// A call to <see cref="UseRouting(IApplicationBuilder)"/> must be followed by a call to | ||
/// <see cref="UseEndpoints(IApplicationBuilder, Action{IEndpointRouteBuilder})"/> for the same <see cref="IApplicationBuilder"/> | ||
/// instance. | ||
/// </para> | ||
/// <para> | ||
/// The <see cref="EndpointRoutingMiddleware"/> defines a point in the middleware pipeline where routing decisions are | ||
/// made, and an <see cref="Endpoint"/> is associated with the <see cref="HttpContext"/>. The <see cref="EndpointMiddleware"/> | ||
/// defines a point in the middleware pipeline where the current <see cref="Endpoint"/> is executed. Middleware between | ||
/// the <see cref="EndpointRoutingMiddleware"/> and <see cref="EndpointMiddleware"/> may observe or change the | ||
/// <see cref="Endpoint"/> associated with the <see cref="HttpContext"/>. | ||
/// </para> | ||
/// </remarks> | ||
public static IApplicationBuilder UseRouting(this IApplicationBuilder builder, bool overrideEndpointRouteBuilder) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. overrideEndpointRouteBuilder..... is literal. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Alternative ideas:
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think if we agree that this approach makes sense, we can discuss the naming in the API review. I think I like the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The bool doesn't seem like a meaningful public parameter, anyone that wants the 'true' behavior will already be calling This should be a separate method name and the bool would be an implementation detail. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we not make this an extension method and leave it as a plain old static method? I don't expect many people to want to call this overload. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This was actually one of my earlier ideas! I wanted to call it app.ReRoute() or app.RerunRouting() or app.UseRerouting(). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the idea of a different name. ResumeRouting? |
||
{ | ||
if (builder == null) | ||
{ | ||
throw new ArgumentNullException(nameof(builder)); | ||
} | ||
|
||
VerifyRoutingServicesAreRegistered(builder); | ||
|
||
var endpointRouteBuilder = new DefaultEndpointRouteBuilder(builder); | ||
builder.Properties[EndpointRouteBuilder] = endpointRouteBuilder; | ||
IEndpointRouteBuilder endpointRouteBuilder; | ||
if (overrideEndpointRouteBuilder || !builder.Properties.TryGetValue(EndpointRouteBuilder, out var routeBuilder)) | ||
{ | ||
endpointRouteBuilder = new DefaultEndpointRouteBuilder(builder); | ||
builder.Properties[EndpointRouteBuilder] = endpointRouteBuilder; | ||
} | ||
else | ||
{ | ||
endpointRouteBuilder = (IEndpointRouteBuilder)routeBuilder!; | ||
} | ||
|
||
return builder.UseMiddleware<EndpointRoutingMiddleware>(endpointRouteBuilder); | ||
} | ||
|
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.
What's the purpose of this property? Trying to grok where it is used...
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.
Nevermind! I see you are using this in the extension methods to check if they are invoked on a WebApplicationBuilder. Correct?
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.
Yes, I added this "hack" to clearly indicate that the builder is a WebApplicationBuilder, where we make use of "creative" pipeline construction techniques 😄