#DotNet #AspNetCore folks using #MinimalApis, anyone has any idea if I can somehow intercept parameter binding/deserialization errors, in order to provide a more complete error response, rather than an empty 400?
Comments
Log in with your Bluesky account to leave a comment
So it seems that what I want is something like the InvalidModelStateResponseFactory, but that's only available when using controllers, not minimal APIs... sad š¢ https://github.com/dotnet/aspnetcore/issues/57337
@captainsafia.bsky.social, @davidfowl.com, @damianedwards.com if you can point me in the right direction, or clarify if I'm out of luck, I appreciate it š
You can intercept the response with a middleware, or to intercept the incoming use a parameter that implements custom binding via a static BindAsync method.
Thanks. Though Iām not seeing how to work with that to include more information in the response. For example, if I had some int query parameters, that the client sent as strings, is there someway to know what failed to bind in order add it to the response?
You can't customize responses from binding failures. If you want to be involved in binding you have to take it over completely with a custom type that implements IBindableFromHttpContext. You could possibly make params nullable and check for null values and return a custom response
For example if I pass a string where it should be an int, or a value that doesn't fit a given enum, I'd like to indicate exactly the parameter(s) (be it query, route, property in the body, etc) that have issues, like we do for other types of validation.
That may be a problem sure, but not the one I'm seeing, cause I get a 400, not a 404, so I assume it's choosing the route correctly and failing to bind the parameters.
Comments
https://github.com/dotnet/aspnetcore/issues/57337
Since you can have the same url take a string or an int and it would be 2 separate routes?