Frage

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?

War es hilfreich?

Lösung

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)

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top