ASP.NET Core 3.1如何获取控制器以使用System.Text.Json进行序列化和反序列化 [英] ASP.NET Core 3.1 How can I get the controller to use System.Text.Json to serialize and deserialize

查看:375
本文介绍了ASP.NET Core 3.1如何获取控制器以使用System.Text.Json进行序列化和反序列化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从.NET CORE 2.2转换为3.1.是否可以强制控制器使用System.Text.Json对任何传入的请求和响应进行序列化和反序列化?基本上,当请求进入时,我想使用System.Text.Json,而响应消失时,我想使用System.Text.Json.如果是,怎么办?

I am converting from .NET CORE 2.2 to 3.1. Is it possible to force the controller to use System.Text.Json to serialize and deserialize any incoming request and response? Basically, when the request comes in I want to use System.Text.Json and when a response goes out I want to use System.Text.Json. If yes, how?

之所以这样做,是因为Microsoft确实在推动该库更安全,更快捷地替代Newtonsoft.Json.但是,我似乎无法在Microsoft的页面上找到任何反映此情况的文档.我发现微软很难更新其代码以利用这个新库.

The reason for doing this is that Microsoft is really pushing this library as the replacement for Newtonsoft.Json as being so much safer and faster. But, I cannot seem to find any documentation on Microsoft's pages that reflects this. I find it hard that Microsoft would not update their code to utilize this new library.

更新

我无法通过使用application/vnd.api+json进行解析来获取System.Text.Json来绑定模型-模型为NULL.仅当我使用application/json进行解析时,它才会绑定.这是有问题的,因为JSON:API规范要求application/vnd.api+json(请参见此处: https://stackoverflow.com/a/61452011/4630376 ).我尝试用[Consumes("application/vnd.api+json")]装饰控制器,但这不起作用.

I am unable to get System.Text.Json to bind the model by parsing with application/vnd.api+json - the model is NULL. It will only bind if I parse using application/json. This is problematic because the JSON:API specification requires application/vnd.api+json (see here: https://stackoverflow.com/a/61452011/4630376). I tried decorating the controller with [Consumes("application/vnd.api+json")], but that does not work.

如何使用application/vnd.api+json使System.Text.Json绑定模型?我最初提出这个问题的假设是.NET Core 3.1没有使用System.Text.Json.由于除了几个评论外,没有人提供答案,所以我选择扩大这个问题.

How do I get System.Text.Json to bind models using application/vnd.api+json? My initial assumption in asking this question is that .NET Core 3.1 was not using System.Text.Json. Since no one has provided an answer, other than a few comments, I have opted to expand this question.

ChangePassword模型:

using System.ComponentModel.DataAnnotations;
using System.Text.Json.Serialization;

namespace ABC.Model.Clients
{
    public class ChangePassword
    {   
        public ChangePassword() { }

        [Required]
        [JsonPropertyName("old-password")]
        public string OldPassword { get; set; }

        [Required]
        [JsonPropertyName("new-password")]
        public string NewPassword { get; set; }

        [Required]
        [JsonPropertyName("confirm-password")]
        public string ConfirmPassword { get; set; }
    }
}

邮递员请求:

{ 
    "data": {
        "attributes": {
            "old-password" : "**********",
            "new-password" : "**********",
            "confirm-password" : "**********"
        },
        "type": "change-passwords"
    }
}

推荐答案

.NET Core 3.0默认情况下使用System.Text.Json.还要检查

.NET Core 3.0 by default uses System.Text.Json. Check also the official documentation article where this is described.

有关.NET Core 3.0中对application/vnd.api+json JSON:API 的支持.我不知道在ASP.NET Core中对此有任何支持.这是与普通Web API分开的标准.您不能只是将其添加为您接受并期望其工作的另一种类型.

Regarding the application/vnd.api+json and the JSON:API support in .NET Core 3.0. I am not aware of any support for this in ASP.NET Core. This is a separate standard from plain web API. You cannot just add it as another type you accept and expect it to work.

我在GitHub JSON API .Net Core 上找到了一个项目,该项目提供了框架用于构建 JSON:API 兼容的Web API服务. .NET Core 3.0的版本目前仍在Alpha中,当前使用的不是Newtonsoft.Json.您可以查看源代码,以了解如何处理此类请求,并可能基于该请求构建自己的解决方案.或加入该项目并帮助他们制作一个System.Text.Json版本.

I found a project on GitHub JSON API .Net Core that provides a framework for building JSON:API compliant web API services. The version for .NET Core 3.0 is at the moment still in Alpha and currently uses Newtonsoft.Json which is not what you want. You can look at the source to see how to handle such requests and maybe build your own solution based on it. Or join the project and help them to make also a System.Text.Json version.

这篇关于ASP.NET Core 3.1如何获取控制器以使用System.Text.Json进行序列化和反序列化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆