Search code examples
c#reactjs.netenums

Calling C# enum to React Component


I am using enum in my .Net Core Web API to list types of cuisine in my project such as

public enum CuisineClass
{
    American = 1,
    British = 2,
    Chinese = 3
}

and so on.

At this point when I'm mapping my GetRestaurantDto and calling my endpoint from in my react SPA, I get the int instead of the string.

Is there a way to get the string instead?


Solution

  • You can use JsonStringEnumConverter. In .NET 5 use it this way:

    services.AddControllers()
    .AddJsonOptions(o =>
    {
        o.JsonSerializerOptions.Converters.Add(new JSonStringEnumConverter());
    });