The reason for this error is that "System.Text.Json doesn't deserialize non-string values into string properties" (source).
That means if you have a controller with the simple [FromBody] string argument:
[HttpPost("save")]
public async Task Save([FromBody] string content)
{
this request will succeed:
curl -H "Content-Type: application/json" -X POST -d "\"abcdefgh\"" https://localhost:5000/save -v
but this will fail:
curl -H "Content-Type: application/json" -X POST -d "{\"content\":\"abcdefgh\"}" https://localhost:5000/save -v
In fact, a similar error occurs not only for string but for other simple types like int, bool, etc. For example, if you change the argument type to int in the code above, then sending JSON {"content":123} in the body will give JSON value could not be converted to System.Int32 error.
To avoid this error either:
- fix the request to pass the argument in the body as
"some string" (instead of JSON)
- pass argument in request as [FromQuery] or [FromForm]
- or move your argument into the property of some class (don't forget the getter and setter for this member because class fields are not deserialized):
public class Content
{
public string Value { get; set;}
}
...
[HttpPost("save")]
public async Task Save([FromBody] Content content)
{
Tested on ASP.NET Core 7.0