If you are returning and receiving the data in Json, you can use StringEnumConverter. Defining a model like this:
class Output
{
[JsonProperty(ItemConverterType = typeof(StringEnumConverter))]
public MyEnum[] Enums { get; set; }
}
public HttpResponseMessage Get()
{
return Request.CreateResponse(HttpStatusCode.OK, new Output { Enums = new MyEnum[] { MyEnum.Someting, MyEnum.Someting2 } }); ;
}
Allows you instruct the json serializer to converts enums to and from its name string value.
Moreover, if you do not want to define the output model, in your WebApiConfig.Register(config) method, you can add this lines and also works:
var jsonFormatter = config.Formatters.JsonFormatter;
jsonFormatter.SerializerSettings.Converters.Add(new StringEnumConverter());
and for .Net Core Web API use this:
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc().AddJsonOptions( op => {
op.SerializerSettings.Converters.Add(new StringEnumConverter());
});