Search code examples
c#validationasp.net-core.net-corevalidationattribute

ASP.NET Core empty validation string entry in array of errors?


When we have an action, accepting the following argument:

[FromBody][Range(1, 10)] int hello

When validation fails, the object returned has an empty entry, like so:

"type": "https://tools.ietf.org/html/rfc7231#section-6.5.1",
"title": "One or more validation errors occurred.",
"status": 400,
"traceId": "|3b00401-417ccac45f29647d.",
"errors": {
    "": [
        "hello is required."
    ]
}

}

Why is that? Could you refer to the source which is causing this problem? I believe it's tied with reflection, i.e they get the object's properties, but in our case it's a simple int/string object, not a custom type.


Solution

  • You can achieve this by using the ModelBinder attribute.

    For example:

    [ModelBinder(Name = "number")]