Ben d'├ętat

Ben Scott

~/Web API JSON serialization of enums pro-tip

06 Sep 2013

When serializing an enum value in Web API as JSON it will use the integer value of the enum by default. To make it serialize as the ToString() value (eg. UserType.Admin as "Admin" instead of 2) add this to WebApiConfig.cs (in the AppStart directory):

config.Formatters.JsonFormatter.SerializerSettings.Converters.Add(new StringEnumConverter());

comments powered by Disqus