dotnet / dotnet/aspnetcore

Parameters binding removes whitespace

Open
#29,948 14 comments 6 reactions 0 assignees View on GitHub
area-mvc feature-model-binding investigate
Dominant language
C#
Stars
38.4k
Forks
10.9k
Avg merge
2d 10h
Merged PRs (30d)
281

Description

### Describe the bug
Binding parameters of controller methods to HTTP request values e.g. using the `[FromForm]` attribute results in `null` value when the value contains whitespace only.

### To Reproduce
```csharp
[HttpPost]
public IActionResult Post([FromForm] string text = null) { ... }
```

```
POST http://localhost:53777/post HTTP/1.1
Content-Type: application/x-www-form-urlencoded

text=%E2%80%AF
```

(i.e. the value is U+202F Narrow No-Break Space)

**Actual:** The `Post` method is called with `text` being `null`.

**Expected:** The `Post` method is called with `text` being `"\u202f"`, corresponding to the `Request.Form["text"]` value.

The same happens with mapping GET parameters and `Request.Query["text"]`. If this is by design, is there a way to turn it off?

### Further technical details
.NET SDKs installed:
2.2.207 [C:\Program Files\dotnet\sdk]
5.0.200-preview.20614.14 [C:\Program Files\dotnet\sdk]

ASP.NET Core version: 2.2
Visual Studio 16.9.0 Preview 3.0

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.