Model binder change validation property name #62857
Unanswered
petrkasnal
asked this question in
Q&A
Replies: 1 comment
-
Hello @petrkasnal, builder.Services.Configure<ApiBehaviorOptions>(options =>
{
options.InvalidModelStateResponseFactory = context =>
{
var newModelState = new ModelStateDictionary();
foreach (var kvp in context.ModelState)
{
var newKey = kvp.Key.StartsWith("$.") ? kvp.Key[2..] :
kvp.Key.StartsWith("$") ? kvp.Key[1..] : kvp.Key;
foreach (var error in kvp.Value.Errors)
newModelState.AddModelError(newKey, error.ErrorMessage);
}
return new BadRequestObjectResult(new ValidationProblemDetails(newModelState)
{
Status = StatusCodes.Status400BadRequest
});
};
}); This intercepts model state before |
Beta Was this translation helpful? Give feedback.
0 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Uh oh!
There was an error while loading. Please reload this page.
-
Hi
I have a question. We are using System.Text.Json in our .NET WebApi project. When I accidentally send a non-valid json to my API (text to number), it will of course return a validation.
Which is fine, of course. However, the name of the property is a bit of a problem for me. Because in our validations we have the property named normally according to the field.
So my question is can I somehow gracefully remove the dollar (root object) from the property name?
Thank you very much
Beta Was this translation helpful? Give feedback.
All reactions