@@ -203,15 +203,7 @@ private static Expression CreateArgument(ParameterInfo parameter, FactoryContext
203
203
}
204
204
else if ( parameterCustomAttributes . OfType < IFromBodyMetadata > ( ) . FirstOrDefault ( ) is { } bodyAttribute )
205
205
{
206
- if ( factoryContext . JsonRequestBodyType is not null )
207
- {
208
- throw new InvalidOperationException ( "Action cannot have more than one FromBody attribute." ) ;
209
- }
210
-
211
- factoryContext . JsonRequestBodyType = parameter . ParameterType ;
212
- factoryContext . AllowEmptyRequestBody = bodyAttribute . AllowEmpty ;
213
-
214
- return Expression . Convert ( BodyValueExpr , parameter . ParameterType ) ;
206
+ return BindParameterFromBody ( parameter . ParameterType , bodyAttribute . AllowEmpty , factoryContext ) ;
215
207
}
216
208
else if ( parameter . CustomAttributes . Any ( a => typeof ( IFromServiceMetadata ) . IsAssignableFrom ( a . AttributeType ) ) )
217
209
{
@@ -229,10 +221,14 @@ private static Expression CreateArgument(ParameterInfo parameter, FactoryContext
229
221
{
230
222
return BindParameterFromRouteValueOrQueryString ( parameter , parameter . Name , factoryContext ) ;
231
223
}
232
- else
224
+ else if ( parameter . ParameterType . IsInterface )
233
225
{
234
226
return Expression . Call ( GetRequiredServiceMethod . MakeGenericMethod ( parameter . ParameterType ) , RequestServicesExpr ) ;
235
227
}
228
+ else
229
+ {
230
+ return BindParameterFromBody ( parameter . ParameterType , allowEmpty : false , factoryContext ) ;
231
+ }
236
232
}
237
233
238
234
private static Expression CreateMethodCall ( MethodInfo methodInfo , Expression ? target , Expression [ ] arguments ) =>
@@ -627,6 +623,19 @@ private static Expression BindParameterFromRouteValueOrQueryString(ParameterInfo
627
623
return BindParameterFromValue ( parameter , Expression . Coalesce ( routeValue , queryValue ) , factoryContext ) ;
628
624
}
629
625
626
+ private static Expression BindParameterFromBody ( Type parameterType , bool allowEmpty , FactoryContext factoryContext )
627
+ {
628
+ if ( factoryContext . JsonRequestBodyType is not null )
629
+ {
630
+ throw new InvalidOperationException ( "Action cannot have more than one FromBody attribute." ) ;
631
+ }
632
+
633
+ factoryContext . JsonRequestBodyType = parameterType ;
634
+ factoryContext . AllowEmptyRequestBody = allowEmpty ;
635
+
636
+ return Expression . Convert ( BodyValueExpr , parameterType ) ;
637
+ }
638
+
630
639
private static MethodInfo GetMethodInfo < T > ( Expression < T > expr )
631
640
{
632
641
var mc = ( MethodCallExpression ) expr . Body ;
0 commit comments