How to thwart ASP.NET web API deserialization when types do not match

I had a problem with the silent failure of deserialization in the ASP.NET Web API (version 5.1.2). I would like deserialization to throw an error, but I cannot find a configuration for it.

My specific (simplified) case is this. The client application (AngularJS) sends an HTTP POST request to the ASP.NET web API backend. As a payload there is a string chain:

["ABC100", "ABC200", "ABC300"]

However, the server expects a list of integers:

List<int> Ids { get; set; }

As a result, deserialization occurs, the list of identifiers will be empty and there will be no errors.

Ids: []

Of course, it is also necessary to eliminate the discrepancy, but it seems obvious to me that the POST request in this case should fail. How can I do this by default?

+4
1

ModelState.IsValid :

[HttpPost]
[Route("Stuff/Ids/")]
public void PostStuff(List<int> Ids)
{
    if(!ModelState.IsValid)
        throw new Exception("ModelState is not valid.");

    // Carry on...
}

ModelState.IsValid , .

, . , , : - ASP.NET

+1

Source: https://habr.com/ru/post/1617025/


All Articles