aspnetcore icon indicating copy to clipboard operation
aspnetcore copied to clipboard

[Question] Is there a way to properly emulate InvalidModelStateResponseFactory in minimal apis?

Open michaeltg17 opened this issue 6 months ago • 2 comments

Hi.

When trying to migrate some existing endpoints to minimal apis, I discovered that is not as easy as in controllers to change the behaviour of a binding error (and I wanted to return the same responses...).

In controllers you have the InvalidModelStateResponseFactory which you can use to modify the response to these invalid requests:

image

When trying to do the same in minimal apis, I have found two ways, filter or middleware.

With a filter you get the arguments but you don't know why the binding failed (required parameter not provided vs. not parsable) and the arguments have default values (like 0 but you passed a string)

With a middleware you can catch the exception but you only get a message like "Failed to bind parameter "long id" from "this has to be a long"."

So the question is, is there another way so I can return the same response as before when using controllers but using minimal apis?

Thanks.

michaeltg17 avatar Aug 14 '24 19:08 michaeltg17