Skip to content

Commit a5a47b0

Browse files
committed
Fix how to check for empty input
1 parent 752b539 commit a5a47b0

File tree

1 file changed

+6
-2
lines changed

1 file changed

+6
-2
lines changed

src/Mvc/Mvc.Core/src/Formatters/InputFormatter.cs

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
using Microsoft.AspNetCore.Http.Features;
45
using Microsoft.AspNetCore.Mvc.ApiExplorer;
56
using Microsoft.AspNetCore.Mvc.Core;
67

@@ -98,8 +99,11 @@ public virtual Task<InputFormatterResult> ReadAsync(InputFormatterContext contex
9899
throw new ArgumentNullException(nameof(context));
99100
}
100101

101-
var request = context.HttpContext.Request;
102-
if (request.ContentLength == 0)
102+
var hasBody = context.HttpContext.Features.Get<IHttpRequestBodyDetectionFeature>()?.CanHaveBody;
103+
// In case the feature is not registered
104+
hasBody ??= context.HttpContext.Request.ContentLength is not null && context.HttpContext.Request.ContentLength != 0;
105+
106+
if (hasBody == false)
103107
{
104108
if (context.TreatEmptyInputAsDefaultValue)
105109
{

0 commit comments

Comments
 (0)