I have an F# WebAPI project and the parameter for the Post method is always set to null for some reason.
(the names are all anonymized)
Here is the controller:
type TestController() =
inherit ApiController()
member x.Get () =
getValues ()
member x.Post ([<FromBody>] values: Values) =
storeValues values
This is how the record Values is defined as:
type TypeA = {
Id: string
....
}
....
type Values = {
AValues: list<TypeA>
BValues: list<TypeB>
CValues: list<TypeC>
}
And in my view I make a POST call to the API like so:
return $.ajax({
type: "POST",
url: url,
data: JSON.stringify(params),
contentType: "application/json",
beforeSend: function (jqXHR) { jqXHR.setRequestHeader("X-HTTP-Method", type); }
});
where data comes out to:
{
"AValues":[{"Id":"blahblah", ....}],
"BValues":[...],
"CValues":[...]
}
I updated the JS to make data evaluate to:
{
"Values":
{
"AValues":[{"Id":"blahblah", ....}],
"BValues":[...],
"CValues":[...]
}
}
and now the debugger throws a nullreference exception and says that Requests is null (breakpoint is on my record definition).
The problem is that it does hit the Post method in the controller, but the values is always set to null.
I forwarded the issue to Ryan Riley who is expect on both F# and Web API and he said the following: