dotnet / dotnet/aspnetcore

Minimal APIs: `IFormFile?` parameter still throws 'Unexpected request without body"

Open
#64,902 2 comments 3 reactions 2 assignees Claimed by @Youssef1313 View on GitHub
area-minimal
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 5h
Merged PRs (30d)
276

Description

### Is there an existing issue for this?

- [x] I have searched the existing issues

### Describe the bug

If I have:
```cs
public static async Task SetCurrentUserProfileImageAsync(IFormFile? image, [other params])
{
}
```
But the client sends the request without a body, it still throws the following even thought the `IFormFile` is nullable:

```
Microsoft.AspNetCore.Http.BadHttpRequestException: 'Unexpected request without body, failed to bind parameter "IFormFile image" from the request body as form.'
```

### Expected Behavior

If the `IFormFile` is optional then it should not throw. In the example above, a null `IFormFile` means the API calll wants to clear the profile image so a body is not required.

### Steps To Reproduce

_No response_

### Exceptions (if any)

_No response_

### .NET Version

10

### Anything else?

_No response_

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.