0

I have a Controller in a ASP.NET Core WebAPI. On a HttpPost I receive the body of the post via the [FromBody] referenced input variable.

For the purposes of this question, let's assume I'm expecting to receive the following JSON submission with null being a valid value.

{
    "start": null,
    "finish": "Far far away"
}

When submitted and start is not present the C# variable simply reflect a null for start.

How can I go about finding out if the start property was present or not in the HttpPost?

Frank
  • 590
  • 1
  • 8
  • 23

1 Answers1

1

You can use JObject if you want more control over the payload. An example is:

[HttpPost]
[Route("api/test")]
public IHttpActionResult Test(JObject item)
{
    //Check if start is included
    var data = item.ToObject<YourClass>();
    ...

}

With [FromBody] , deserialization happens on the fly, hence difficult to intercept the result.

Felix Too
  • 11,614
  • 5
  • 24
  • 25