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?

有帮助吗?

解决方案

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)

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top