Parameters binding removes whitespace
See original GitHub issueDescribe 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
[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
Issue Analytics
- State:
- Created 3 years ago
- Reactions:2
- Comments:8 (5 by maintainers)
Top Results From Across the Web
White-space parameter value is not a valid input in ...
On API call whitespace gets converted to null on the server (not on the client) In my case whitespace is a valid input....
Read more >[Solved] How do i pass a whitespace as parameter
You're assumuing is correctly when the webservice between the call of the WCF service the whitespace is lost. what type of binding are...
Read more >How to remove trailing spaces from makefile variable?
I know how to remove all white spaces from end of all lines... I can also use "strip" to edit variables... The point...
Read more >How whitespace is handled by HTML, CSS, and in the DOM
Any whitespace characters that are outside of HTML elements in the original document are represented in the DOM. This is needed internally so ......
Read more >String.prototype.trim() - JavaScript - MDN Web Docs
The trim() method of String values removes whitespace from both ends of this string and returns a new string, without modifying the original ......
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found

Just comparing to null would be enough, otherwise we would have the same problem with the empty string.
Note that the values present in
ControllerBase.Requestdo contain whitespace as expected. It can be used as a workaround for this bug.My controller looks something like this:
Example request URL:
https://localhost:6670/myEndpoint?paramWithWhitespace=%20Also, a related stack overflow: https://stackoverflow.com/questions/49797196/asp-net-space-in-query-parameter-value