.NET API Guide: Returning Enum Values as Strings

I was recently asked to return an enum as a string instead of an int from an API call. At first, I considered how to override the framework’s behavior, but after some research, I found the solution was easier than I expected. The easiest way to resolve this is by adding a JsonStringEnumConverter() in the .AddJsonOptions() method of the Startup class:

1
2
3
4
5
6
7
8
.AddNewtonsoftJson(options =>
     {
         options.SerializerSettings.Converters = new List<Newtonsoft.Json.JsonConverter> { new StringEnumConverter() };
     })
     .AddJsonOptions(options =>
     {
         options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
     });

The first part adds a StringEnumConverter to the SerializerSettings.Converters list for Newtonsoft.Json. The second part adds a JsonStringEnumConverter to the JsonSerializerOptions.Converters list for the built-in JSON serializer. This ensures that enums are serialized as strings instead of integers.

Share: X (Twitter) Facebook LinkedIn