Force Framework to use Enum Entity Name instead of its Value

asp.net-mvc, asp.net-web-api, c#, rest

Solution

You can use a JsonConverter.

There is a native one for JSON.Net `StringEnumConverter` mentioned in this question JSON serialization of enum as string

Either anotate your property:

[JsonConverter(typeof(StringEnumConverter))]
public enum ReturnCode { OK, InvalidParameter }

Or use the config examples in WebApi Json.NET custom date handling to register in the global serialiser settings.

Problem

I created a ASP.NET MVC 4 Web application. Within this application i implemented several rest webservices and here comes my question: Is it possible to force the object serialization to use the enum entity names instead of their values? This is my enum: ``` public enum ReturnCode { OK, InvalidParameter } ``` This is what I get: ``` { "returnCode": 0, "data": null } ``` But this is what I want: ``` { "returnCode": OK, "data": null } ``` Is there a way to achieve this?

Original source

Related problems