.net WebAPI [FromBody] receive null unexpectedly?

Rex Ye
1 min readMar 22, 2021

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.

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Rex Ye
Rex Ye

Written by Rex Ye

.net, Angular, React. Creator of epicerp.app

No responses yet

Write a response