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?

Was it helpful?

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)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top