C#C
C#3y 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()
                ));
        });


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
Was this page helpful?