92

How to serialize Enum fields to String instead of an Int in ASP.NET MVC Core 3.0? I'm not able to do it the old way.

services.AddMvc().AddJsonOptions(opts =>
{
    opts.JsonSerializerOptions.Converters.Add(new StringEnumConverter());
})

I'm getting an error:

cannot convert from 'Newtonsoft.Json.Converters.StringEnumConverter' to 'System.Text.Json.Serialization.JsonConverter'

Andrei
  • 42,814
  • 35
  • 154
  • 218

5 Answers5

193

New System.Text.Json serialization

ASP.NET MVC Core 3.0 uses built-in JSON serialization. Use System.Text.Json.Serialization.JsonStringEnumConverter (with "Json" prefix):

services
    .AddMvc()
    // Or .AddControllers(...)
    .AddJsonOptions(opts =>
    {
        var enumConverter = new JsonStringEnumConverter();
        opts.JsonSerializerOptions.Converters.Add(enumConverter);
    })

More info here. The documentation can be found here.

If you prefer Newtonsoft.Json

You can also use "traditional" Newtonsoft.Json serialization:

Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson

And then:

services
    .AddControllers()
    .AddNewtonsoftJson(opts => opts
        .Converters.Add(new StringEnumConverter()));
Andrei
  • 42,814
  • 35
  • 154
  • 218
  • 26
    If you have a Web API then instead of `.AddMvc()` you can also use `services.AddControllers().AddJsonOptions(...)`. – Tobias Feb 27 '20 at 15:26
  • 26
    as of asp.net core 3.1 and Microsoft.AspNetCore.Mvc.NewtonsoftJson 3.1.5, there is a slight change:
    services.AddControllers()
    .AddNewtonsoftJson(opts => opts.SerializerSettings.Converters.Add(new StringEnumConverter()));
    
    – Sudhanshu Mishra Jul 12 '20 at 12:57
  • 1
    what if i dont want to do this accross the bard ? is there a way to do this as an attribute on my dto? – drowhunter Aug 23 '20 at 20:46
  • I found this website to be very helpful: https://www.jasongaylord.com/blog/2020/07/17/adding-newtonsoft-json-to-services – ashlar64 Aug 23 '20 at 21:39
  • 8
    @drowhunter If you want to just do this or a specific property, you can simply annotate the property in the return DTO like this `[JsonConverter(typeof(JsonStringEnumConverter))] public CategoryDto Category { get; set; }`. You will need to import the `using System.Text.Json.Serialization` namespace – Gavin Jun 09 '21 at 07:01
  • Thank you this worked for me `services.AddControllers()..AddJsonOptions(opts => { opts.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()); })` – Qwerty Jun 17 '21 at 10:40
26

some addition:
if use Newtonsoft.Json

Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson
services
    .AddControllers()
    .AddNewtonsoftJson(options =>
        options.SerializerSettings.Converters.Add(new Newtonsoft.Json.Converters.StringEnumConverter()));

options.SerializerSettings.Converters

SerializerSettings is necessary

J. Liu
  • 346
  • 4
  • 4
21

If you have a Minimal API this will be useful:

using System.Text.Json.Serialization;

builder.Services.Configure<Microsoft.AspNetCore.Http.Json.JsonOptions>(opt =>
{
    opt.SerializerOptions.Converters.Add(new JsonStringEnumConverter());
});
giorgi02
  • 683
  • 6
  • 13
9

.NET 7.0 introduces this way:

builder.Services.ConfigureHttpJsonOptions(options => options.SerializerOptions.Converters.Add(new JsonStringEnumConverter()));
asaf92
  • 1,557
  • 1
  • 19
  • 30
1

If you are using Aspnet Core MVC with the minimal API use this:

services.Configure<Microsoft.AspNetCore.Mvc.JsonOptions>(o => o.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter()));
Backs
  • 24,430
  • 5
  • 58
  • 85
Michael Edwards
  • 6,308
  • 6
  • 44
  • 75