@@ -18,13 +18,16 @@ public static class CqrsRouteMapper
18
18
19
19
private static readonly List < Type > CommandTypes = new ( ) { typeof ( ICommand < > ) , typeof ( ICommand < , > ) } ;
20
20
21
+ private static readonly string [ ] GetAndHeadMethods = { "GET" , "HEAD" } ;
22
+
21
23
/// <summary>
22
24
/// Map a query API, using GET method. <typeparamref name="T"/> would been constructed from route and query string.
23
25
/// </summary>
24
26
/// <param name="app"><see cref="IApplicationBuilder"/></param>
25
27
/// <param name="route">The route template for API.</param>
26
28
/// <param name="mapNullableRouteParameters">Multiple routes should be mapped when for nullable route parameters.</param>
27
29
/// <param name="nullRouteParameterPattern">Replace route parameter with given string to represent null.</param>
30
+ /// <param name="enableHead">Map HEAD method for the same routes.</param>
28
31
/// <typeparam name="T">The type of the query.</typeparam>
29
32
/// <returns></returns>
30
33
/// <example>
@@ -44,13 +47,15 @@ public static IEndpointConventionBuilder MapQuery<T>(
44
47
this IEndpointRouteBuilder app ,
45
48
[ StringSyntax ( "Route" ) ] string route ,
46
49
MapNullableRouteParameter mapNullableRouteParameters = MapNullableRouteParameter . Disable ,
47
- string nullRouteParameterPattern = "-" )
50
+ string nullRouteParameterPattern = "-" ,
51
+ bool enableHead = false )
48
52
{
49
53
return app . MapQuery (
50
54
route ,
51
55
( [ AsParameters ] T query ) => query ,
52
56
mapNullableRouteParameters ,
53
- nullRouteParameterPattern ) ;
57
+ nullRouteParameterPattern ,
58
+ enableHead ) ;
54
59
}
55
60
56
61
/// <summary>
@@ -61,6 +66,7 @@ public static IEndpointConventionBuilder MapQuery<T>(
61
66
/// <param name="handler">The delegate that returns a <see cref="IQuery{TView}"/> instance.</param>
62
67
/// <param name="mapNullableRouteParameters">Multiple routes should be mapped when for nullable route parameters.</param>
63
68
/// <param name="nullRouteParameterPattern">Replace route parameter with given string to represent null.</param>
69
+ /// <param name="enableHead">Allow HEAD for the same routes.</param>
64
70
/// <returns></returns>
65
71
/// <example>
66
72
/// The following code:
@@ -80,7 +86,8 @@ public static IEndpointConventionBuilder MapQuery(
80
86
[ StringSyntax ( "Route" ) ] string route ,
81
87
Delegate handler ,
82
88
MapNullableRouteParameter mapNullableRouteParameters = MapNullableRouteParameter . Disable ,
83
- string nullRouteParameterPattern = "-" )
89
+ string nullRouteParameterPattern = "-" ,
90
+ bool enableHead = false )
84
91
{
85
92
var isQuery = handler . Method . ReturnType . GetInterfaces ( ) . Where ( x => x . IsGenericType )
86
93
. Any ( x => QueryTypes . Contains ( x . GetGenericTypeDefinition ( ) ) ) ;
@@ -92,7 +99,7 @@ public static IEndpointConventionBuilder MapQuery(
92
99
93
100
if ( mapNullableRouteParameters is MapNullableRouteParameter . Disable )
94
101
{
95
- return app . MapGet ( route , handler ) . AddEndpointFilter < QueryEndpointHandler > ( ) ;
102
+ return MapRoutes ( route ) ;
96
103
}
97
104
98
105
if ( string . IsNullOrWhiteSpace ( nullRouteParameterPattern ) )
@@ -125,10 +132,16 @@ public static IEndpointConventionBuilder MapQuery(
125
132
var regex = new Regex ( "{" + x . Name + "[^}]*?}" , RegexOptions . IgnoreCase ) ;
126
133
return regex . Replace ( r , nullRouteParameterPattern ) ;
127
134
} ) ;
128
- app . MapGet ( newRoute , handler ) . AddEndpointFilter < QueryEndpointHandler > ( ) ;
135
+ MapRoutes ( newRoute ) ;
129
136
}
130
137
131
- return app . MapGet ( route , handler ) . AddEndpointFilter < QueryEndpointHandler > ( ) ;
138
+ return MapRoutes ( route ) ;
139
+
140
+ IEndpointConventionBuilder MapRoutes ( string r )
141
+ {
142
+ var endpoint = enableHead ? app . MapMethods ( r , GetAndHeadMethods , handler ) : app . MapGet ( r , handler ) ;
143
+ return endpoint . AddEndpointFilter < QueryEndpointHandler > ( ) ;
144
+ }
132
145
}
133
146
134
147
/// <summary>
0 commit comments