ASP.NET MVC Core API Serialize Enums to String

前端 未结 2 468
北海茫月
北海茫月 2020-12-30 20:54

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          


        
2条回答
  •  时光说笑
    2020-12-30 21:12

    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

提交回复
热议问题