Skip to content

Commit 9571d6a

Browse files
committed
Fix RequestDelegate being chosen over Delegate in overload resolution
Attempt to resolve the issue where the RequestDelegate overload is incorrectly preferred over the Delegate overload by using the new OverloadResolutionPriority attribute.
1 parent 39f1443 commit 9571d6a

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

src/Http/Routing/src/Builder/EndpointRouteBuilderExtensions.cs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.Diagnostics.CodeAnalysis;
5+
using System.Runtime.CompilerServices;
56
using Microsoft.AspNetCore.Http;
67
using Microsoft.AspNetCore.Routing;
78
using Microsoft.AspNetCore.Routing.Patterns;
@@ -227,6 +228,7 @@ static RequestDelegateResult CreateHandlerRequestDelegate(Delegate handler, Requ
227228
/// <returns>A <see cref="RouteHandlerBuilder"/> that can be used to further customize the endpoint.</returns>
228229
[RequiresUnreferencedCode(MapEndpointUnreferencedCodeWarning)]
229230
[RequiresDynamicCode(MapEndpointDynamicCodeWarning)]
231+
[OverloadResolutionPriority(1)]
230232
public static RouteHandlerBuilder MapGet(
231233
this IEndpointRouteBuilder endpoints,
232234
[StringSyntax("Route")] string pattern,
@@ -245,6 +247,7 @@ public static RouteHandlerBuilder MapGet(
245247
/// <returns>A <see cref="RouteHandlerBuilder"/> that can be used to further customize the endpoint.</returns>
246248
[RequiresUnreferencedCode(MapEndpointUnreferencedCodeWarning)]
247249
[RequiresDynamicCode(MapEndpointDynamicCodeWarning)]
250+
[OverloadResolutionPriority(1)]
248251
public static RouteHandlerBuilder MapPost(
249252
this IEndpointRouteBuilder endpoints,
250253
[StringSyntax("Route")] string pattern,
@@ -263,6 +266,7 @@ public static RouteHandlerBuilder MapPost(
263266
/// <returns>A <see cref="RouteHandlerBuilder"/> that can be used to further customize the endpoint.</returns>
264267
[RequiresUnreferencedCode(MapEndpointUnreferencedCodeWarning)]
265268
[RequiresDynamicCode(MapEndpointDynamicCodeWarning)]
269+
[OverloadResolutionPriority(1)]
266270
public static RouteHandlerBuilder MapPut(
267271
this IEndpointRouteBuilder endpoints,
268272
[StringSyntax("Route")] string pattern,
@@ -281,6 +285,7 @@ public static RouteHandlerBuilder MapPut(
281285
/// <returns>A <see cref="RouteHandlerBuilder"/> that can be used to further customize the endpoint.</returns>
282286
[RequiresUnreferencedCode(MapEndpointUnreferencedCodeWarning)]
283287
[RequiresDynamicCode(MapEndpointDynamicCodeWarning)]
288+
[OverloadResolutionPriority(1)]
284289
public static RouteHandlerBuilder MapDelete(
285290
this IEndpointRouteBuilder endpoints,
286291
[StringSyntax("Route")] string pattern,
@@ -299,6 +304,7 @@ public static RouteHandlerBuilder MapDelete(
299304
/// <returns>A <see cref="RouteHandlerBuilder"/> that can be used to further customize the endpoint.</returns>
300305
[RequiresUnreferencedCode(MapEndpointUnreferencedCodeWarning)]
301306
[RequiresDynamicCode(MapEndpointDynamicCodeWarning)]
307+
[OverloadResolutionPriority(1)]
302308
public static RouteHandlerBuilder MapPatch(
303309
this IEndpointRouteBuilder endpoints,
304310
[StringSyntax("Route")] string pattern,
@@ -338,6 +344,7 @@ public static RouteHandlerBuilder MapMethods(
338344
/// <returns>A <see cref="RouteHandlerBuilder"/> that can be used to further customize the endpoint.</returns>
339345
[RequiresUnreferencedCode(MapEndpointUnreferencedCodeWarning)]
340346
[RequiresDynamicCode(MapEndpointDynamicCodeWarning)]
347+
[OverloadResolutionPriority(1)]
341348
public static RouteHandlerBuilder Map(
342349
this IEndpointRouteBuilder endpoints,
343350
[StringSyntax("Route")] string pattern,
@@ -413,6 +420,7 @@ public static RouteHandlerBuilder MapFallback(this IEndpointRouteBuilder endpoin
413420
/// </remarks>
414421
[RequiresUnreferencedCode(MapEndpointUnreferencedCodeWarning)]
415422
[RequiresDynamicCode(MapEndpointDynamicCodeWarning)]
423+
[OverloadResolutionPriority(1)]
416424
public static RouteHandlerBuilder MapFallback(
417425
this IEndpointRouteBuilder endpoints,
418426
[StringSyntax("Route")] string pattern,

src/Http/Routing/src/Builder/FallbackEndpointRouteBuilderExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
using System.Diagnostics.CodeAnalysis;
55
using Microsoft.AspNetCore.Http;
66
using Microsoft.AspNetCore.Routing;
7+
using Microsoft.AspNetCore.Routing.Patterns;
78

89
namespace Microsoft.AspNetCore.Builder;
910

@@ -74,7 +75,7 @@ public static IEndpointConventionBuilder MapFallback(
7475
ArgumentNullException.ThrowIfNull(pattern);
7576
ArgumentNullException.ThrowIfNull(requestDelegate);
7677

77-
var conventionBuilder = endpoints.Map(pattern, requestDelegate);
78+
var conventionBuilder = endpoints.Map(RoutePatternFactory.Parse(pattern), requestDelegate);
7879
conventionBuilder.WithDisplayName("Fallback " + pattern);
7980
conventionBuilder.Add(b => ((RouteEndpointBuilder)b).Order = int.MaxValue);
8081
conventionBuilder.WithMetadata(FallbackMetadata.Instance);

src/Http/Routing/src/ShortCircuit/RouteShortCircuitEndpointRouteBuilderExtensions.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
using Microsoft.AspNetCore.Builder;
55
using Microsoft.AspNetCore.Http;
6+
using Microsoft.AspNetCore.Routing.Patterns;
67

78
namespace Microsoft.AspNetCore.Routing;
89

@@ -34,7 +35,7 @@ public static IEndpointConventionBuilder MapShortCircuit(this IEndpointRouteBuil
3435
{
3536
route = $"{routePrefix}/{{**catchall}}";
3637
}
37-
group.Map(route, _shortCircuitDelegate)
38+
group.Map(RoutePatternFactory.Parse(route), _shortCircuitDelegate)
3839
.ShortCircuit(statusCode)
3940
.Add(endpoint =>
4041
{

0 commit comments

Comments
 (0)