I have logic like this in my .Net Core API controller that checks for bad values sent by a front-end:
[HttpPut("{id}")]
public async Task<IActionResult> PutMathCoordinates(MathCoordinates)
if ((MathCoordinates.GraphX == 0) && (MathCoordinates.GraphY == 0))
{
// send message that data is bad
}
But even with that, I get some 400 errors like this:
400 The JSON value could not be converted to System.Guid
When the front-end sends empty data like this:
{problemId: "", mathId: "", graphX: 0, graphY: 0}
Why isn't my controller catching that in my if
statement?
Thanks!
CodePudding user response:
- Your input for the method is missing a declaration, it should be something like
MathCoordinates input
which you then refer asinput.GraphX
- You're trying to post a GUID to a method requiring an object of type
MathCoordinates
. Either you need to change the input parameters and fetch the model yourself based on the GUID parameter, or post JSON representing the body so JSON can automatically serialize the JSON to the model type (requires [FromBody] on the input).