✅ Enums as strings on AspNetCore controller .NET 9
I have added this enum
In swagger it shows as expected

6 Replies
However when you send a request it fails because of model validation

form data does not use encode using json
you want something like [TypeConverter(typeof(EnumMemberConverter<Locale>))] and [EnumMember(Value = "ae_AE")]
see https://stackoverflow.com/a/68625960
Stack Overflow
Can't map [FromForm] enum with [EnumMember] in asp.net core api 3.1
I have an enum with EnumMemberAttribute specified for its values:
public enum HireStrategy
{
[EnumMember(Value = "30-days")]
Days30,
[EnumMember(Value = "60-days")]
...
but for localization there is already support in aspnet to grab it from various places https://learn.microsoft.com/en-us/aspnet/core/fundamentals/localization?view=aspnetcore-9.0
Globalization and localization in ASP.NET Core
Learn how ASP.NET Core provides services and middleware for localizing content into different languages and cultures.
Thanks man, sorted the problem instantly. Added support to the converter to take into account JsonStringEnumMemberNameAttribute, so that this converted and json converter will convert in the same way across my app.
Thanks but no thanks, I have a spec I need to follow 🙂
For any potential future readers here's my solution: