Skip to content

Commit 78973a2

Browse files
authored
Remove MapAction overload (#30563)
1 parent 9b5ce87 commit 78973a2

12 files changed

+52
-356
lines changed

src/Http/Routing/samples/MapActionSample/Startup.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,10 +28,9 @@ public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
2828

2929
app.UseEndpoints(endpoints =>
3030
{
31-
[HttpPost("/EchoTodo")]
3231
JsonResult EchoTodo([FromBody] Todo todo) => new(todo);
3332

34-
endpoints.MapAction((Func<Todo, JsonResult>)EchoTodo);
33+
endpoints.MapPost("/EchoTodo", (Func<Todo, JsonResult>)EchoTodo);
3534

3635
endpoints.MapPost("/EchoTodoProto", async httpContext =>
3736
{

src/Http/Routing/src/Builder/MapActionEndpointRouteBuilderExtensions.cs

+2-96
Original file line numberDiff line numberDiff line change
@@ -22,68 +22,6 @@ public static class MapActionEndpointRouteBuilderExtensions
2222
private static readonly string[] PutVerb = new[] { "PUT" };
2323
private static readonly string[] DeleteVerb = new[] { "DELETE" };
2424

25-
/// <summary>
26-
/// Adds a <see cref="RouteEndpoint"/> to the <see cref="IEndpointRouteBuilder"/> that matches the pattern specified via attributes.
27-
/// </summary>
28-
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/> to add the route to.</param>
29-
/// <param name="action">The delegate executed when the endpoint is matched.</param>
30-
/// <returns>An <see cref="IEndpointConventionBuilder"/> that can be used to further customize the endpoint.</returns>
31-
public static MapActionEndpointConventionBuilder MapAction(
32-
this IEndpointRouteBuilder endpoints,
33-
Delegate action)
34-
{
35-
if (endpoints is null)
36-
{
37-
throw new ArgumentNullException(nameof(endpoints));
38-
}
39-
40-
if (action is null)
41-
{
42-
throw new ArgumentNullException(nameof(action));
43-
}
44-
45-
var requestDelegate = MapActionExpressionTreeBuilder.BuildRequestDelegate(action);
46-
47-
var routeAttributes = action.Method.GetCustomAttributes().OfType<IRoutePatternMetadata>();
48-
var conventionBuilders = new List<IEndpointConventionBuilder>();
49-
50-
const int defaultOrder = 0;
51-
52-
foreach (var routeAttribute in routeAttributes)
53-
{
54-
if (routeAttribute.RoutePattern is not string pattern)
55-
{
56-
continue;
57-
}
58-
59-
var routeName = (routeAttribute as IRouteNameMetadata)?.RouteName;
60-
var routeOrder = (routeAttribute as IRouteOrderMetadata)?.RouteOrder;
61-
62-
var conventionBuilder = endpoints.Map(pattern, requestDelegate);
63-
64-
conventionBuilder.Add(endpointBuilder =>
65-
{
66-
foreach (var attribute in action.Method.GetCustomAttributes())
67-
{
68-
endpointBuilder.Metadata.Add(attribute);
69-
}
70-
71-
endpointBuilder.DisplayName = routeName ?? pattern;
72-
73-
((RouteEndpointBuilder)endpointBuilder).Order = routeOrder ?? defaultOrder;
74-
});
75-
76-
conventionBuilders.Add(conventionBuilder);
77-
}
78-
79-
if (conventionBuilders.Count == 0)
80-
{
81-
throw new InvalidOperationException("Action must have a pattern. Is it missing a Route attribute?");
82-
}
83-
84-
return new MapActionEndpointConventionBuilder(conventionBuilders);
85-
}
86-
8725
/// <summary>
8826
/// Adds a <see cref="RouteEndpoint"/> to the <see cref="IEndpointRouteBuilder"/> that matches HTTP GET requests
8927
/// for the specified pattern.
@@ -168,8 +106,8 @@ public static MapActionEndpointConventionBuilder MapMethods(
168106
throw new ArgumentNullException(nameof(httpMethods));
169107
}
170108

171-
var displayName = $"{pattern} HTTP: {string.Join(", ", httpMethods)}";
172-
var builder = endpoints.Map(RoutePatternFactory.Parse(pattern), action, displayName);
109+
var builder = endpoints.Map(RoutePatternFactory.Parse(pattern), action);
110+
builder.WithDisplayName($"{pattern} HTTP: {string.Join(", ", httpMethods)}");
173111
builder.WithMetadata(new HttpMethodMetadata(httpMethods));
174112
return builder;
175113
}
@@ -202,15 +140,6 @@ public static MapActionEndpointConventionBuilder Map(
202140
this IEndpointRouteBuilder endpoints,
203141
RoutePattern pattern,
204142
Delegate action)
205-
{
206-
return Map(endpoints, pattern, action, displayName: null);
207-
}
208-
209-
private static MapActionEndpointConventionBuilder Map(
210-
this IEndpointRouteBuilder endpoints,
211-
RoutePattern pattern,
212-
Delegate action,
213-
string? displayName)
214143
{
215144
if (endpoints is null)
216145
{
@@ -239,39 +168,16 @@ private static MapActionEndpointConventionBuilder Map(
239168

240169
// Add delegate attributes as metadata
241170
var attributes = action.Method.GetCustomAttributes();
242-
string? routeName = null;
243-
int? routeOrder = null;
244171

245172
// This can be null if the delegate is a dynamic method or compiled from an expression tree
246173
if (attributes is not null)
247174
{
248175
foreach (var attribute in attributes)
249176
{
250-
if (attribute is IRoutePatternMetadata patternMetadata && patternMetadata.RoutePattern is not null)
251-
{
252-
throw new InvalidOperationException($"'{attribute.GetType()}' implements {nameof(IRoutePatternMetadata)} which is not supported by this method.");
253-
}
254-
if (attribute is IHttpMethodMetadata methodMetadata && methodMetadata.HttpMethods.Any())
255-
{
256-
throw new InvalidOperationException($"'{attribute.GetType()}' implements {nameof(IHttpMethodMetadata)} which is not supported by this method.");
257-
}
258-
259-
if (attribute is IRouteNameMetadata nameMetadata && nameMetadata.RouteName is string name)
260-
{
261-
routeName = name;
262-
}
263-
if (attribute is IRouteOrderMetadata orderMetadata && orderMetadata.RouteOrder is int order)
264-
{
265-
routeOrder = order;
266-
}
267-
268177
builder.Metadata.Add(attribute);
269178
}
270179
}
271180

272-
builder.DisplayName = routeName ?? displayName ?? builder.DisplayName;
273-
builder.Order = routeOrder ?? defaultOrder;
274-
275181
var dataSource = endpoints.DataSources.OfType<ModelEndpointDataSource>().FirstOrDefault();
276182
if (dataSource is null)
277183
{

src/Http/Routing/src/IRouteOrderMetadata.cs

-19
This file was deleted.

src/Http/Routing/src/IRoutePatternMetadata.cs

-16
This file was deleted.

src/Http/Routing/src/PublicAPI.Unshipped.txt

-5
Original file line numberDiff line numberDiff line change
@@ -12,15 +12,10 @@ Microsoft.AspNetCore.Routing.DataTokensMetadata.DataTokens.get -> System.Collect
1212
Microsoft.AspNetCore.Routing.DataTokensMetadata.DataTokensMetadata(System.Collections.Generic.IReadOnlyDictionary<string!, object?>! dataTokens) -> void
1313
Microsoft.AspNetCore.Routing.IDataTokensMetadata.DataTokens.get -> System.Collections.Generic.IReadOnlyDictionary<string!, object?>!
1414
Microsoft.AspNetCore.Routing.IRouteNameMetadata.RouteName.get -> string?
15-
Microsoft.AspNetCore.Routing.IRouteOrderMetadata
16-
Microsoft.AspNetCore.Routing.IRouteOrderMetadata.RouteOrder.get -> int?
17-
Microsoft.AspNetCore.Routing.IRoutePatternMetadata
18-
Microsoft.AspNetCore.Routing.IRoutePatternMetadata.RoutePattern.get -> string?
1915
Microsoft.AspNetCore.Routing.RouteNameMetadata.RouteName.get -> string?
2016
Microsoft.AspNetCore.Routing.RouteNameMetadata.RouteNameMetadata(string? routeName) -> void
2117
static Microsoft.AspNetCore.Builder.MapActionEndpointRouteBuilderExtensions.Map(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder! endpoints, Microsoft.AspNetCore.Routing.Patterns.RoutePattern! pattern, System.Delegate! action) -> Microsoft.AspNetCore.Builder.MapActionEndpointConventionBuilder!
2218
static Microsoft.AspNetCore.Builder.MapActionEndpointRouteBuilderExtensions.Map(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder! endpoints, string! pattern, System.Delegate! action) -> Microsoft.AspNetCore.Builder.MapActionEndpointConventionBuilder!
23-
static Microsoft.AspNetCore.Builder.MapActionEndpointRouteBuilderExtensions.MapAction(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder! endpoints, System.Delegate! action) -> Microsoft.AspNetCore.Builder.MapActionEndpointConventionBuilder!
2419
static Microsoft.AspNetCore.Builder.MapActionEndpointRouteBuilderExtensions.MapDelete(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder! endpoints, string! pattern, System.Delegate! action) -> Microsoft.AspNetCore.Builder.MapActionEndpointConventionBuilder!
2520
static Microsoft.AspNetCore.Builder.MapActionEndpointRouteBuilderExtensions.MapGet(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder! endpoints, string! pattern, System.Delegate! action) -> Microsoft.AspNetCore.Builder.MapActionEndpointConventionBuilder!
2621
static Microsoft.AspNetCore.Builder.MapActionEndpointRouteBuilderExtensions.MapMethods(this Microsoft.AspNetCore.Routing.IEndpointRouteBuilder! endpoints, string! pattern, System.Collections.Generic.IEnumerable<string!>! httpMethods, System.Delegate! action) -> Microsoft.AspNetCore.Builder.MapActionEndpointConventionBuilder!

src/Http/Routing/test/FunctionalTests/MapActionTest.cs

-42
Original file line numberDiff line numberDiff line change
@@ -19,48 +19,6 @@ namespace Microsoft.AspNetCore.Routing.FunctionalTests
1919
{
2020
public class MapActionTest
2121
{
22-
[Fact]
23-
public async Task MapAction_FromBodyWorksWithJsonPayload()
24-
{
25-
[HttpPost("/EchoTodo/{id}")]
26-
Todo EchoTodo([FromRoute] int id, [FromBody] Todo todo) => todo with { Id = id };
27-
28-
using var host = new HostBuilder()
29-
.ConfigureWebHost(webHostBuilder =>
30-
{
31-
webHostBuilder
32-
.Configure(app =>
33-
{
34-
app.UseRouting();
35-
app.UseEndpoints(b => b.MapAction((Func<int, Todo, Todo>)EchoTodo));
36-
})
37-
.UseTestServer();
38-
})
39-
.ConfigureServices(services =>
40-
{
41-
services.AddRouting();
42-
})
43-
.Build();
44-
45-
using var server = host.GetTestServer();
46-
await host.StartAsync();
47-
var client = server.CreateClient();
48-
49-
var todo = new Todo
50-
{
51-
Name = "Write tests!"
52-
};
53-
54-
var response = await client.PostAsJsonAsync("/EchoTodo/42", todo);
55-
response.EnsureSuccessStatusCode();
56-
57-
var echoedTodo = await response.Content.ReadFromJsonAsync<Todo>();
58-
59-
Assert.NotNull(echoedTodo);
60-
Assert.Equal(todo.Name, echoedTodo?.Name);
61-
Assert.Equal(42, echoedTodo?.Id);
62-
}
63-
6422
[Fact]
6523
public async Task MapPost_FromBodyWorksWithJsonPayload()
6624
{

0 commit comments

Comments
 (0)