C
C#17mo ago
barcode

❔ Issues with model state validation

Hello, I'm trying to disable model state validation so I can return responses without the standard msdn response
builder.Services.Configure<ApiBehaviorOptions>(options =>
{
options.SuppressModelStateInvalidFilter = true;
options.InvalidModelStateResponseFactory = actionContext =>
throw new BadRequestException(string.Join(
Environment.NewLine,
actionContext.ModelState.Values.SelectMany(v => v.Errors.Select(x => x.ErrorMessage))
.ToArray()
));
});
builder.Services.Configure<ApiBehaviorOptions>(options =>
{
options.SuppressModelStateInvalidFilter = true;
options.InvalidModelStateResponseFactory = actionContext =>
throw new BadRequestException(string.Join(
Environment.NewLine,
actionContext.ModelState.Values.SelectMany(v => v.Errors.Select(x => x.ErrorMessage))
.ToArray()
));
});
BadRequestException is my exception which is supposed to be caught inside an api filter, this works fine. The main issue is that I can't trigger the invalidmodelstatefactory, If i say suppressModel = false it just doesn't enter the factory and returns 500 internal error frombody is null, and if I say suppressModel = true then I just get the standard msdn response
1 Reply
Accord
Accord17mo ago
Looks like nothing has happened here. I will mark this as stale and this post will be archived until there is new activity.
Want results from more Discord servers?
Add your server