Question

I have an action that looks thus:

[HttpPost]
public HttpResponseMessage PostInstantiation(Guid id, [FromBody]Instantiation instantiation)

If the user does a POST to the correct URL, but with no (/empty) body, then model validation doesn't fail, and the instantiation argument is null. I expected it to fail.

What is the correct way of handling this scenario?

Était-ce utile?

La solution

Yeah, right...what you are noticing is an expected behavior. If you indeed would like to check if the user has sent an empty body, then you could probably check for the Content-Length header of the incoming request.

if(Request.Content.Headers.ContentLength == 0)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top