DEV Community

loading...

.net WebAPI [FromBody] receive null unexpectedly?

rexebin profile image Rex ・1 min read

I promised myself I would write a blog post if I could find out why one of my controllers kept receiving null when I clearly sent a matching body in the request to it. And here I am, after 30 or so minutes of trying and experimenting, I found out why: I have a Guid? property, and I send an empty string for it! Dotnet tried to parse the empty string to Guid and failed, so it silently returns null. That is it. There isn’t much to write about at all!

This might be an idiotic mistake on my part, but .net did not give me any useful information. It simply gives me a null. I think .net could do better and throw me a JSON parsing error instead.

A note to myself and dear readers, when the .net API endpoint returns null instead of the intended object, check the property types of the model against what is sent in the HTTP request. If there is no apparent mismatch, one way to find the culprit is to remove properties on the model, one at a time.

Discussion (0)

Forem Open with the Forem app