@@ -42,10 +42,56 @@ public static IApplicationBuilder UseRouting(this IApplicationBuilder builder)
42
42
throw new ArgumentNullException ( nameof ( builder ) ) ;
43
43
}
44
44
45
+ return UseRouting ( builder , true ) ;
46
+ }
47
+
48
+ /// <summary>
49
+ /// Adds a <see cref="EndpointRoutingMiddleware"/> middleware to the specified <see cref="IApplicationBuilder"/>.
50
+ /// </summary>
51
+ /// <param name="builder">The <see cref="IApplicationBuilder"/> to add the middleware to.</param>
52
+ /// <param name="overrideEndpointRouteBuilder">Whether a new <see cref="EndpointRouteBuilder"/> should be created.</param>
53
+ /// <returns>A reference to this instance after the operation has completed.</returns>
54
+ /// <remarks>
55
+ /// <para>
56
+ /// A call to <see cref="UseRouting(IApplicationBuilder)"/> must be followed by a call to
57
+ /// <see cref="UseEndpoints(IApplicationBuilder, Action{IEndpointRouteBuilder})"/> for the same <see cref="IApplicationBuilder"/>
58
+ /// instance.
59
+ /// </para>
60
+ /// <para>
61
+ /// The <see cref="EndpointRoutingMiddleware"/> defines a point in the middleware pipeline where routing decisions are
62
+ /// made, and an <see cref="Endpoint"/> is associated with the <see cref="HttpContext"/>. The <see cref="EndpointMiddleware"/>
63
+ /// defines a point in the middleware pipeline where the current <see cref="Endpoint"/> is executed. Middleware between
64
+ /// the <see cref="EndpointRoutingMiddleware"/> and <see cref="EndpointMiddleware"/> may observe or change the
65
+ /// <see cref="Endpoint"/> associated with the <see cref="HttpContext"/>.
66
+ /// </para>
67
+ /// </remarks>
68
+ public static IApplicationBuilder UseRouting ( this IApplicationBuilder builder , bool overrideEndpointRouteBuilder )
69
+ {
70
+ if ( builder == null )
71
+ {
72
+ throw new ArgumentNullException ( nameof ( builder ) ) ;
73
+ }
74
+
45
75
VerifyRoutingServicesAreRegistered ( builder ) ;
46
76
47
- var endpointRouteBuilder = new DefaultEndpointRouteBuilder ( builder ) ;
48
- builder . Properties [ EndpointRouteBuilder ] = endpointRouteBuilder ;
77
+ IEndpointRouteBuilder endpointRouteBuilder ;
78
+ if ( overrideEndpointRouteBuilder )
79
+ {
80
+ endpointRouteBuilder = new DefaultEndpointRouteBuilder ( builder ) ;
81
+ builder . Properties [ EndpointRouteBuilder ] = endpointRouteBuilder ;
82
+ }
83
+ else
84
+ {
85
+ if ( builder . Properties . TryGetValue ( EndpointRouteBuilder , out var routeBuilder ) )
86
+ {
87
+ endpointRouteBuilder = ( IEndpointRouteBuilder ) routeBuilder ! ;
88
+ }
89
+ else
90
+ {
91
+ endpointRouteBuilder = new DefaultEndpointRouteBuilder ( builder ) ;
92
+ builder . Properties [ EndpointRouteBuilder ] = endpointRouteBuilder ;
93
+ }
94
+ }
49
95
50
96
return builder . UseMiddleware < EndpointRoutingMiddleware > ( endpointRouteBuilder ) ;
51
97
}
0 commit comments