3

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?

John
  • 33
  • 4

2 Answers2

1

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

`services.AddControllers()
.AddJsonOptions(o =>
 {
o.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
});`
mojajoja
  • 58
  • 1
  • 5
  • 1
    Perfect! I can't believe that's all that was needed. Tried so many different things. It's working just like I wanted to. Thanks for your help! – John Feb 17 '21 at 14:16
1

Why return the full name? If you are using typescript you can setup the enum on your reactjs application too and handle it as such.

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

You can then access the strings from TS if you want How to get names of enum entries?

Athanasios Kataras
  • 25,191
  • 4
  • 32
  • 61
  • I'm not using Typescript unfortunately. The solution above is all I needed. Thanks for your reply though! – John Feb 17 '21 at 14:17