question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Parameters binding removes whitespace

See original GitHub issue

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

[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:open
  • Created 3 years ago
  • Reactions:2
  • Comments:8 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
pensonocommented, Jun 28, 2022

Just comparing to null would be enough, otherwise we would have the same problem with the empty string.

2reactions
pensonocommented, Jun 23, 2022

Note that the values present in ControllerBase.Request do contain whitespace as expected. It can be used as a workaround for this bug.

My controller looks something like this:

[HttpGet("myEndpoint")]
public async Task<ActionResult> GetEndpoint([FromQuery] string? paramWithWhitespace)
{
    // This treats whitespace correctly https://github.com/dotnet/aspnetcore/issues/29948
    paramWithWhitespace = Request.Query[nameof(paramWithWhitespace)];

    // ...
}

Example request URL: https://localhost:6670/myEndpoint?paramWithWhitespace=%20

Also, a related stack overflow: https://stackoverflow.com/questions/49797196/asp-net-space-in-query-parameter-value

Read more comments on GitHub >

github_iconTop 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 >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found