Introduce a more ergonomic way of enabling case-insensitive enum parsing in Minimal API endpoints
- Dominant language
- C#
- Stars
- 38.4k
- Forks
- 10.9k
- Avg merge
- 2d 10h
- Merged PRs (30d)
- 281
Description
Basically the same as #45590.
Currently, the only way to make this work would be to create another type that acts as a wrapper around the enum, just to implement a `TryParse` method that does case-insensitive parsing for the underlying enum; as @captainsafia described in the aforementioned issue:
> You might want to consider implementing your own TryParse implementation on a wrapper for your MyEnum type that is case-insensitive. See more info at https://learn.microsoft.com/en-us/aspnet/core/fundamentals/minimal-apis/parameter-binding?view=aspnetcore-7.0#tryparse
But it goes without saying that this is incredibly ugly.
So, either:
- When binding enum parameters in minimal API handlers, use the `Enum.TryParse` overload that does a case-insensitive comparison. This way the behavior will also be consistent with that of MVC controllers. Or provide a global opt-in option that lets the developer decide.
- Provide a simpler way to customize parameter binding on an individual-parameter-basis for minimal API handlers without having to define a `TryParse` or `BindAsync` method on the **type** of the parameter. This is a direly needed feature in general; as you don't always have access to the type of a parameter in such a way that you can declare a static `TryParse` or `BindAsync` method on them (as is effectively the case for enums, or for types that come from third-party libraries), so it should be possible to customize binding for individual parameters in a granular fashion, as opposed to having to modify the types themselves.
The second option is of course preferable.
Contributor guide
Assessment
This issue has not been assessed yet.