I have a controller action like this:
public Foo Get([FromUri] Filter[] filters){...}
With an Enum like so:
public enum Filter
{
Yes,
No,
FileNotFound
}
When I make a call to my controller with the following request:
/api/things?filters=garbagevalue
Then the values passed to the controller is Filter.Yes
(the default value). Is it possible to get webapi to reject, perhaps a BadRequest or other response code and/or exception, any request to a controller with an enum parameter where the enum parameter value is unknown? Alternatively, is it possible to filter out values from the query string parameters which do not match enum values?
I understand that it might be better to have a None
value in the enum and detect that, but I'd like to avoid having to add that to all of my enums. Also, None
would then be a valid value, which would have to be detected, and I wouldn't be able to give any feedback to the person making the request which query parameter was invalid.
Thanks!
Use a nullable enum type:
public Foo Get([FromUri] Filter?[] filters){...}
The unknown/garbage values in the array will be null.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With