Seerex
Seerex

Reputation: 601

Request parameter is null?

I am having trouble receiving a simple JSON request with ASP.NET Core 5. For whatever reason, it is not binding my JSON and passing it into the parameter in the action.

Here is my json:

{
    "name": "John",
    "age": 31,
    "city": "New York"
}

Here is my controller:

[HttpPost]
public IActionResult SubtractCurrency([FromBody] JObject data)
{
    return Json(data);
}

Can anyone tell me what I am doing wrong? Currently I am just trying to print the entire data out, but it is just null. I can easily send a request with a simple string and receive like a string and that works fine.

What am I missing?

Upvotes: 2

Views: 1179

Answers (1)

Alvin Chung
Alvin Chung

Reputation: 324

System.Text.Json, the default JSON Serializer/Deserializer from .Net Core 3.0 cannot parse JSON to JToken (like JObject, JArray) through the request body directly.

You can change it back to Newtonsoft.Json instead

  1. Install Microsoft.AspNetCore.Mvc.NewtonsoftJson in NuGet
  2. Go to ConfigureServices() in Startup.cs
  3. Add .AddNewtonsoftJson() after the services.AddControllers()

Result: services.AddControllers().AddNewtonsoftJson(); result

Upvotes: 2

Related Questions