@@ -1475,44 +1475,48 @@ private static Expression AddResponseWritingToMethodCall(Expression methodCall,
1475
1475
object ? formValue = null ;
1476
1476
var feature = httpContext . Features . Get < IHttpRequestBodyDetectionFeature > ( ) ;
1477
1477
1478
- if ( feature ? . CanHaveBody == true )
1478
+ if ( feature ? . CanHaveBody == false )
1479
1479
{
1480
- if ( httpContext . Features . Get < IAntiforgeryValidationFeature > ( ) is { IsValid : false } antiforgeryValidationFeature )
1481
- {
1482
- Log . InvalidAntiforgeryToken ( httpContext , parameterTypeName , parameterName , antiforgeryValidationFeature . Error ! , throwOnBadRequest ) ;
1483
- httpContext . Response . StatusCode = StatusCodes . Status400BadRequest ;
1484
- return ( null , false ) ;
1485
- }
1480
+ Log . UnexpectedRequestWithoutBody ( httpContext , parameterTypeName , parameterName , throwOnBadRequest ) ;
1481
+ httpContext . Response . StatusCode = StatusCodes . Status400BadRequest ;
1482
+ return ( null , false ) ;
1483
+ }
1486
1484
1487
- if ( ! httpContext . Request . HasFormContentType )
1488
- {
1489
- Log . UnexpectedNonFormContentType ( httpContext , httpContext . Request . ContentType , throwOnBadRequest ) ;
1490
- httpContext . Response . StatusCode = StatusCodes . Status415UnsupportedMediaType ;
1491
- return ( null , false ) ;
1492
- }
1485
+ if ( httpContext . Features . Get < IAntiforgeryValidationFeature > ( ) is { IsValid : false } antiforgeryValidationFeature )
1486
+ {
1487
+ Log . InvalidAntiforgeryToken ( httpContext , parameterTypeName , parameterName , antiforgeryValidationFeature . Error ! , throwOnBadRequest ) ;
1488
+ httpContext . Response . StatusCode = StatusCodes . Status400BadRequest ;
1489
+ return ( null , false ) ;
1490
+ }
1493
1491
1494
- try
1495
- {
1496
- formValue = await httpContext . Request . ReadFormAsync ( ) ;
1497
- }
1498
- catch ( BadHttpRequestException ex )
1499
- {
1500
- Log . RequestBodyIOException ( httpContext , ex ) ;
1501
- httpContext . Response . StatusCode = ex . StatusCode ;
1502
- return ( null , false ) ;
1503
- }
1504
- catch ( IOException ex )
1505
- {
1506
- Log . RequestBodyIOException ( httpContext , ex ) ;
1507
- httpContext . Response . StatusCode = StatusCodes . Status400BadRequest ;
1508
- return ( null , false ) ;
1509
- }
1510
- catch ( InvalidDataException ex )
1511
- {
1512
- Log . InvalidFormRequestBody ( httpContext , parameterTypeName , parameterName , ex , throwOnBadRequest ) ;
1513
- httpContext . Response . StatusCode = StatusCodes . Status400BadRequest ;
1514
- return ( null , false ) ;
1515
- }
1492
+ if ( ! httpContext . Request . HasFormContentType )
1493
+ {
1494
+ Log . UnexpectedNonFormContentType ( httpContext , httpContext . Request . ContentType , throwOnBadRequest ) ;
1495
+ httpContext . Response . StatusCode = StatusCodes . Status415UnsupportedMediaType ;
1496
+ return ( null , false ) ;
1497
+ }
1498
+
1499
+ try
1500
+ {
1501
+ formValue = await httpContext . Request . ReadFormAsync ( ) ;
1502
+ }
1503
+ catch ( BadHttpRequestException ex )
1504
+ {
1505
+ Log . RequestBodyIOException ( httpContext , ex ) ;
1506
+ httpContext . Response . StatusCode = ex . StatusCode ;
1507
+ return ( null , false ) ;
1508
+ }
1509
+ catch ( IOException ex )
1510
+ {
1511
+ Log . RequestBodyIOException ( httpContext , ex ) ;
1512
+ httpContext . Response . StatusCode = StatusCodes . Status400BadRequest ;
1513
+ return ( null , false ) ;
1514
+ }
1515
+ catch ( InvalidDataException ex )
1516
+ {
1517
+ Log . InvalidFormRequestBody ( httpContext , parameterTypeName , parameterName , ex , throwOnBadRequest ) ;
1518
+ httpContext . Response . StatusCode = StatusCodes . Status400BadRequest ;
1519
+ return ( null , false ) ;
1516
1520
}
1517
1521
1518
1522
return ( formValue , true ) ;
@@ -2739,6 +2743,20 @@ public static void FormDataMappingFailed(HttpContext httpContext, string paramet
2739
2743
[ LoggerMessage ( RequestDelegateCreationLogging . FormDataMappingFailedEventId , LogLevel . Debug , RequestDelegateCreationLogging . FormDataMappingFailedLogMessage , EventName = RequestDelegateCreationLogging . FormDataMappingFailedEventName ) ]
2740
2744
private static partial void FormDataMappingFailed ( ILogger logger , string parameterType , string parameterName , Exception exception ) ;
2741
2745
2746
+ public static void UnexpectedRequestWithoutBody ( HttpContext httpContext , string parameterTypeName , string parameterName , bool shouldThrow )
2747
+ {
2748
+ if ( shouldThrow )
2749
+ {
2750
+ var message = string . Format ( CultureInfo . InvariantCulture , RequestDelegateCreationLogging . UnexpectedRequestWithoutBodyExceptionMessage , parameterTypeName , parameterName ) ;
2751
+ throw new BadHttpRequestException ( message ) ;
2752
+ }
2753
+
2754
+ UnexpectedRequestWithoutBody ( GetLogger ( httpContext ) , parameterTypeName , parameterName ) ;
2755
+ }
2756
+
2757
+ [ LoggerMessage ( RequestDelegateCreationLogging . UnexpectedRequestWithoutBodyEventId , LogLevel . Debug , RequestDelegateCreationLogging . UnexpectedRequestWithoutBodyLogMessage , EventName = RequestDelegateCreationLogging . UnexpectedRequestWithoutBodyEventName ) ]
2758
+ private static partial void UnexpectedRequestWithoutBody ( ILogger logger , string parameterType , string parameterName ) ;
2759
+
2742
2760
private static ILogger GetLogger ( HttpContext httpContext )
2743
2761
{
2744
2762
var loggerFactory = httpContext . RequestServices . GetRequiredService < ILoggerFactory > ( ) ;
0 commit comments