JsonIgnore 属性在 ASP.NET Core 3 中保持序列化属性
JsonIgnore attribute keeps serializing properties in ASP.NET Core 3
我最近将我的 API 项目更新为 ASP.NET Core 3。从那时起,[JsonIgnore]
属性不起作用:
public class Diagnostico
{
[JsonIgnore]
public int TipoDiagnostico { get; set; }
[JsonIgnore]
public int Orden { get; set; }
[JsonIgnore]
public DateTime? FechaInicio { get; set; }
public string TipoCodificacion { get; set; }
public string Codigo { get; set; }
public string Descripcion { get; set; }
}
类 的所有属性正在序列化。 API 端点在 .NET Core 3 中,但所有逻辑都在 .NET Standard 2.1 中。我意识到序列化程序已从 Newtonsoft.Json
更改为
System.Text.Json
。此包在 .NET Standard 2.1 中不可用(它仅适用于 .NET Core)因此要在 .NET Standard 项目中的模型中使用 [JsonIgnore]
我正在使用 Newtonsoft.Json
.
[JsonIgnore]
是一个 JSON.NET 属性,不会被新的 System.Text.Json
序列化程序使用。
由于您的应用程序是 ASP.NET Core 3.0 System.Text.Json
将默认使用。如果你想继续使用JSON.NET注解,你必须在ASP.NET Core 3
中使用JSON.NET
就像将 .AddNewtonsoftJson()
添加到您的 MVC 或 WebApi Builder 一样简单
services.AddMvc()
.AddNewtonsoftJson();
或
services.AddControllers()
.AddNewtonsoftJson();
用于类似 WebAPI 的应用程序。
对于您的 .Net Standard 项目,从 nuget
获取 System.Text.Json 包
https://www.nuget.org/packages/System.Text.Json
所以你可以使用 System.Text.Json.Serialization.JsonIgnoreAttribute 而不是 Newtonsoft.Json.JsonIgnoreAttribute.
我最近将我的 API 项目更新为 ASP.NET Core 3。从那时起,[JsonIgnore]
属性不起作用:
public class Diagnostico
{
[JsonIgnore]
public int TipoDiagnostico { get; set; }
[JsonIgnore]
public int Orden { get; set; }
[JsonIgnore]
public DateTime? FechaInicio { get; set; }
public string TipoCodificacion { get; set; }
public string Codigo { get; set; }
public string Descripcion { get; set; }
}
类 的所有属性正在序列化。 API 端点在 .NET Core 3 中,但所有逻辑都在 .NET Standard 2.1 中。我意识到序列化程序已从 Newtonsoft.Json
更改为
System.Text.Json
。此包在 .NET Standard 2.1 中不可用(它仅适用于 .NET Core)因此要在 .NET Standard 项目中的模型中使用 [JsonIgnore]
我正在使用 Newtonsoft.Json
.
[JsonIgnore]
是一个 JSON.NET 属性,不会被新的 System.Text.Json
序列化程序使用。
由于您的应用程序是 ASP.NET Core 3.0 System.Text.Json
将默认使用。如果你想继续使用JSON.NET注解,你必须在ASP.NET Core 3
就像将 .AddNewtonsoftJson()
添加到您的 MVC 或 WebApi Builder 一样简单
services.AddMvc()
.AddNewtonsoftJson();
或
services.AddControllers()
.AddNewtonsoftJson();
用于类似 WebAPI 的应用程序。
对于您的 .Net Standard 项目,从 nuget
获取 System.Text.Json 包https://www.nuget.org/packages/System.Text.Json
所以你可以使用 System.Text.Json.Serialization.JsonIgnoreAttribute 而不是 Newtonsoft.Json.JsonIgnoreAttribute.