dotnet / dotnet/aspnetcore

Index Binding doesnt work with IFormFile but works with other types.

Open
#44,398 6 comments 2 reactions 0 assignees View on GitHub
area-mvc feature-model-binding
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 6h
Merged PRs (30d)
290

Description

### Is there an existing issue for this?

- [X] I have searched the existing issues

### Describe the bug

Consider the following controller:
```cs
public IEnumerable Post([FromForm] List files)
{
return files.Select(x => x);
}
```

You can send the following form data and have the controller method's parameters be populated as expected:
```
files[0]: A
files[1]: B
```

Output:
```
200 - OK
[
"A",
"B"
]
```

However, with `IFormFile`, this behavior does not work:
```cs
public IEnumerable Post([FromForm] List files)
{
return files.Select(x => x.Name);
}
```

Form Data:
```
files[0]: file1.txt
files[1]: file2.txt
```

Output:
`files` in the controller action is empty.

```txt
POST /api/post HTTP/1.1
User-Agent: PostmanRuntime/7.29.2
Accept: */*
Postman-Token: 58d71a1f-3861-4dfd-96cf-51a492d925de
Host: localhost:7197
Accept-Encoding: gzip, deflate, br
Connection: keep-alive
Content-Type: multipart/form-data; boundary=--------------------------154424746869067791626589
Content-Length: 198608

----------------------------154424746869067791626589
Content-Disposition: form-data; name="files[0]"; filename="file1.txt"

----------------------------154424746869067791626589
Content-Disposition: form-data; name="files[1]"; filename="file2.txt"

----------------------------154424746869067791626589--

HTTP/1.1 200 OK
Content-Type: application/json; charset=utf-8
Date: Thu, 06 Oct 2022 14:06:07 GMT
Server: Kestrel
Transfer-Encoding: chunked

[]
```

I know that you don't need indexers as you can select multiple files under the same key however, one of our clients is PHP 5 (which cannot be upgraded easily for various reasons), and before PHP 7, HTTP client libraries cannot send form data keys that are unique. While this is a bug with the version of PHP we are running, I would still expect indexing behavior to persist across all types.

### Expected Behavior

```cs
public IEnumerable Post([FromForm] List files)
{
return files.Select(x => x.Name);
}
```

Form Data:
```
files[0]: file1.txt
files[1]: file2.txt
```

Output:
```
200 - OK
[
"file1.txt",
"file2.txt"
]
```

### Steps To Reproduce

*Explained above*

### Exceptions (if any)

_No response_

### .NET Version

.NET 6

### 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.