Let's say I have this model:
public enum State
{
Valid = 1,
Invalid = 2
}
public class Person
{
public string Name { get; set; }
public State state { get; set; }
}
And this controller action:
[HttpPost]
public object SavePerson([FromBody] Person person)
{
return person;
}
If I send this JSON, everything works just fine:
{
"name": "John",
"state": 1
}
However, if I change the "state": 1 to an invalid enumeration like "state": "" or "state": "1", then the person parameter would be null.
In other words, if I send a JSON that is partially valid, ASP.NET Core ignores all fields.
How can I configure ASP.NET Core to at least extract the valid fields from the body?
Newtonsoft.Json. We're usingSystem.Text.Json.System.Text.Jsondoesn't have error handler. github.com/dotnet/runtime/issues/38049, github.com/dotnet/runtime/issues/44390System.Text.Jsonmay not do what you want. You can useNewtonsoft.Jsonor try a custom formatter.