I\'m still learning web API, so pardon me if my question sounds stupid.
I have this in my StudentController
:
public HttpResponseMessage
I've hit this problem so many times, but actually, it's quite straightforward to track down the cause.
Here's today's example. I was calling my POST service with an AccountRequest
object, but when I put a breakpoint at the start of this function, the parameter value was always null
. But why ?!
[ProducesResponseType(typeof(DocumentInfo[]), 201)]
[HttpPost]
public async Task Post([FromBody] AccountRequest accountRequest)
{
// At this point... accountRequest is null... but why ?!
// ... other code ...
}
To identify the problem, change the parameter type to string
, add a line to get JSON.Net
to deserialize the object into the type you were expecting, and put a breakpoint on this line:
[ProducesResponseType(typeof(DocumentInfo[]), 201)]
[HttpPost]
public async Task Post([FromBody] string ar)
{
// Put a breakpoint on the following line... what is the value of "ar" ?
AccountRequest accountRequest = JsonConvert.DeserializeObject(ar);
// ... other code ...
}
Now, when you try this, if the parameter is still blank or null
, then you simply aren't calling the service properly.
However, if the string does contain a value, then the DeserializeObject
should point you towards the cause of the problem, and should also fail to convert your string into your desired format. But with the raw (string
) data which it's trying to deserialize, you should now be able to see what's wrong with your parameter value.
(In my case, we were calling the service with an AccountRequest
object which had been accidentally serialized twice !)