JsonIgnore属性保留ASP.NET Core 3中的序列化属性

编程入门 行业动态 更新时间:2024-10-27 18:23:37
本文介绍了JsonIgnore属性保留ASP.NET Core 3中的序列化属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我最近将我的API项目更新为ASP.NET Core3.此后,[JsonIgnore]属性不起作用:

I've recently updated my API project to ASP.NET Core 3. Since then, [JsonIgnore] attributes are not working:

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中有效),因此要在我正在使用Newtonsoft.Json的.NET Standard项目内的模型中使用[JsonIgnore].

All the properties of classes are being serialized. The API endpoints are in .NET Core 3, but all the logic is in .NET Standard 2.1. I have realized that the serializer has changed from Newtonsoft.Json to System.Text.Json. This package is not available in .NET Standard 2.1 (it only works in .NET Core) so to use [JsonIgnore] in Models inside .NET Standard projects I am using Newtonsoft.Json .

推荐答案

[JsonIgnore]是JSON.NET属性,新的System.Text.Json序列化程序将不会使用它.

[JsonIgnore] is an JSON.NET attribute and won't be used by the new System.Text.Json serializer.

由于您的应用程序是ASP.NET Core 3.0,因此默认情况下将使用System.Text.Json.如果要继续使用JSON.NET批注,则必须在ASP.NET Core 3中使用JSON.NET

Since your application is an ASP.NET Core 3.0 System.Text.Json will be used by default. If you want to continue to consume the JSON.NET annotations, you have to use JSON.NET in ASP.NET Core 3

就像将.AddNewtonsoftJson()添加到您的MVC或WebApi Builder中一样简单

It's as easy as adding .AddNewtonsoftJson() to your MVC or WebApi Builder

services.AddMvc() .AddNewtonsoftJson();

services.AddControllers() .AddNewtonsoftJson();

适用于WebAPI的应用程序.

for WebAPI-esque applications.

更多推荐

JsonIgnore属性保留ASP.NET Core 3中的序列化属性

本文发布于:2023-11-13 23:28:12,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1585612.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:属性   序列化   JsonIgnore   ASP   Core

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!