I encountered this interesting (frustrating) problem recently after upgrading from .NET Core 2.2 to 3.1. Previously I would POST data to the web app, with the receiving method looking something like this:
public IActionResult OnPostAddNewDataAsync([FromBody]MyClass data)
{
//...
}
where MyClass might be something like this:
public class MyClass
{
public string Field1 {get; set;}
public integer Value1 {get; set;}
}
(For reference, to hit the endpoint from Javascript, you can do it like this:
await PostDataAsync("AddNewData", {
"Field1": "Hello I am",
"Value1": 7
});
async function PostDataAsync(handler, data)
{
return await $.ajax({
url: window.location.pathname + "?handler=" + handler,
type: "POST",
contentType: "application/json",
headers:
{
RequestVerificationToken: $('input:hidden[name="__RequestVerificationToken"]').val()
},
data: JSON.stringify(data)
});
}
it took me a while to figure that out!).
This worked fine.
After updating .NET Core 3.1, many of my POST methods stopped working, and the [FromBody] value would be null.
Note: As part of my upgrade to .NET Core 3.1 I removed references to Newtonsoft.Json, deciding instead to try and use the new System.Text.Json. This would turn out to be important!