-
Notifications
You must be signed in to change notification settings - Fork 9.9k
/
RouteHandlerServices.cs
56 lines (52 loc) · 2.55 KB
/
RouteHandlerServices.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System.ComponentModel;
using System.Reflection;
using Microsoft.AspNetCore.Builder;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing.Patterns;
namespace Microsoft.AspNetCore.Routing;
/// <summary>
/// Provides methods used for invoking the route endpoint
/// infrastructure with custom funcs for populating metadata
/// and creating request delegates. Intended to be consumed from
/// the RequestDeleatgeGenerator only.
/// </summary>
[EditorBrowsable(EditorBrowsableState.Never)]
public static class RouteHandlerServices
{
/// <summary>
/// Registers an endpoint with custom functions for constructing
/// a request delegate for its handler and populating metadata for
/// the endpoint. Intended for consumption in the RequestDelegateGenerator.
/// </summary>
/// <param name="endpoints">The <see cref="IEndpointRouteBuilder"/> to add the route to.</param>
/// <param name="pattern">The route pattern.</param>
/// <param name="handler">The delegate executed when the endpoint is matched.</param>
/// <param name="httpMethods">The set of supported HTTP methods.</param>
/// <param name="populateMetadata">A delegate for populating endpoint metadata.</param>
/// <param name="createRequestDelegate">A delegate for constructing a RequestDelegate.</param>
/// <returns></returns>
public static RouteHandlerBuilder Map(
IEndpointRouteBuilder endpoints,
string pattern,
Delegate handler,
IEnumerable<string>? httpMethods,
Func<MethodInfo, RequestDelegateFactoryOptions?, RequestDelegateMetadataResult> populateMetadata,
Func<Delegate, RequestDelegateFactoryOptions, RequestDelegateMetadataResult?, RequestDelegateResult> createRequestDelegate)
{
ArgumentNullException.ThrowIfNull(endpoints);
ArgumentNullException.ThrowIfNull(pattern);
ArgumentNullException.ThrowIfNull(handler);
ArgumentNullException.ThrowIfNull(populateMetadata);
ArgumentNullException.ThrowIfNull(createRequestDelegate);
return endpoints
.GetOrAddRouteEndpointDataSource()
.AddRouteHandler(RoutePatternFactory.Parse(pattern),
handler,
httpMethods,
isFallback: false,
populateMetadata,
createRequestDelegate);
}
}