109

How to serialize Enum fields to String instead of an Int in ASP.NET Core 3.0 MVC? 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'

1
  • ASP.NET Core 3.0 doesn't include JSON.NET which means you had to add the package explicitly before trying this code Commented Nov 29, 2019 at 8:43

5 Answers 5

217

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()));
Sign up to request clarification or add additional context in comments.

6 Comments

If you have a Web API then instead of .AddMvc() you can also use services.AddControllers().AddJsonOptions(...).
as of asp.net core 3.1 and Microsoft.AspNetCore.Mvc.NewtonsoftJson 3.1.5, there is a slight change: <pre> services.AddControllers() .AddNewtonsoftJson(opts => opts.SerializerSettings.Converters.Add(new StringEnumConverter())); </pre>
what if i dont want to do this accross the bard ? is there a way to do this as an attribute on my dto?
I found this website to be very helpful: jasongaylord.com/blog/2020/07/17/…
@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
|
29

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

Comments

28

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());
});

Comments

22

.NET 7.0 introduces this way:

builder.Services.ConfigureHttpJsonOptions(options => options.SerializerOptions.Converters.Add(new JsonStringEnumConverter()));

1 Comment

Only this worked in my recent .net 9 app.
3

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()));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.