相关疑难解决方法(0)

使用在启动时全局设置的 JsonStringEnumConverter 排除模型的枚举属性?

我正在使用最新的 .NET Core 3.1.1 开发 ASP.NET Core 应用程序,而System.Text.Json该应用程序之前使用的是Newtonsoft.Json. 按照 Microsoft迁移指南中的建议 ,我已经完成了更改。另外,由于我的大多数枚举需要序列化为字符串,因此我已将 Startup.cs 配置ConfigureServices为全局使用JsonStringEnumConverter

public void ConfigureServices(IServiceCollection services)
{
    // lines omitted for brevity

    services.AddControllers()
                .AddJsonOptions(options =>
                    {
                        options.JsonSerializerOptions.Converters.Add(new JsonStringEnumConverter());
                        options.JsonSerializerOptions.IgnoreNullValues = true;
                        options.JsonSerializerOptions.PropertyNameCaseInsensitive = true;
                    });
}
Run Code Online (Sandbox Code Playgroud)

但最近,在发布之后,我们意识到只有少数枚举通过我们的 API 以 json 形式给出。由于这些 API 是外部使用的,因此将数字更改为字符串可能是一件代价高昂的事情。

那么,有没有办法忽略某些枚举属性的通用性,例如带有属性的装饰[JsonIgnore]

c# json .net-core asp.net-core system.text.json

6
推荐指数
1
解决办法
3566
查看次数

标签 统计

.net-core ×1

asp.net-core ×1

c# ×1

json ×1

system.text.json ×1