当我使用下划线符号时,属性 JsonProperty 在 .NET Core 3.1 中工作不正确 [英] Attribute JsonProperty works incorrect with .NET Core 3.1 when I use underscore symbol

查看:30
本文介绍了当我使用下划线符号时,属性 JsonProperty 在 .NET Core 3.1 中工作不正确的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下 JSON 用于补丁请求:

I have the following JSON for patch request:

{
    "idfa": "28A427FE-770B-4FA3-AA8E-123",
    "idfv": "11B3343C-ECBB-4CC8123B5BA-DDD9CA5768FD",
    "app_build_number": 1,
    "app_version": "1.0.0",
    "screen_height": 820,
    "screen_width": 300,
    "locale": "ru",
    "app_id": "com.hello",
    "app_platform": "iOS",
    "manufacturer": "Apple",
    "model": "iPhone10,6",
    "os_version": "12.3.1",
    "sdk_version": "0.3"
}

以及以下用于映射的模型:

And the following model for mapping:

    public class CustomerChangeViewModel
    {
        [JsonProperty("idfa")]
        [Required(ErrorMessage = "Required idfa")]
        public string Idfa { get; set; }

        [JsonProperty("idfv")]
        [Required(ErrorMessage = "Required idfv")]
        public string Idfv { get; set; }

        [Required(ErrorMessage = "Required app_build_number")]
        [JsonProperty("app_build_number")]
        public string AppBuildNumber { get; set; }

        [JsonProperty("app_version")]
        [Required(ErrorMessage = "Required app_version")]
        public string AppVersion { get; set; }

        [JsonProperty("screen_height")]
        [Required(ErrorMessage = "Required screen_height")]
        public string ScreenHeight { get; set; }

        [JsonProperty("screen_width")]
        [Required(ErrorMessage = "Required width")]
        public string ScreenWidth { get; set; }

        [JsonProperty("locale")]
        [Required(ErrorMessage = "Required locale")]
        public string Locale { get; set; }

        [JsonProperty("app_id")]
        [Required(ErrorMessage = "Required app_id")]
        public string AppId { get; set; }

        [JsonProperty("app_platform")]
        [Required(ErrorMessage = "Required app_platform")]
        public string AppPlatform { get; set; }

        [JsonProperty("manufacturer")]
        [Required(ErrorMessage = "Required manufacturer")]
        public string Manufacturer { get; set; }

        [JsonProperty("model")]
        [Required(ErrorMessage = "Required model")]
        public string Model { get; set; }

        [JsonProperty("os_version")]
        [Required(ErrorMessage = "Required os_version")]
        public string OsVersion { get; set; }

        [JsonProperty("sdk_version")]
        [Required(ErrorMessage = "Required sdk_version")]
        public string SdkVersion { get; set; }
    }

和控制器:

[Route("/api/v1.0/startup")]
[HttpPatch]
public async Task<IActionResult> Update([FromBody] CustomerChangeViewModel viewModel)
{
    ...
}

发送此请求后,我有以下内容:

After sending this request I have the following:

如您所见,并非所有字段都已映射.我认为带有_"符号的字段有问题.任何想法为什么会发生?我使用 .NET Core 3.1 和 Insomnia 作为 HTTP 客户端.

As you can see not all fields were mapped. I think there is a problem with fields with "_" symbol. Any ideas why it's happening? I use .NET Core 3.1 and Insomnia as HTTP client.

P.S 我不确定这里是否有必要,但我的路由设置是:

P.S I'm not sure is it necessary here, but my routing settings is:

app.UseRouting();

app.UseEndpoints(endpoints =>
{
       endpoints.MapControllers();
});

推荐答案

.NET Core 3.* 默认使用 System.Text.Json 而它不适用于 JsonPropertyAttribute 类.

.NET Core 3.* is using System.Text.Json by default and it doesn't work with JsonPropertyAttribute class.

您需要安装Microsoft.AspNetCore.Mvc.NewtonsoftJson.

Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson

并调用AddNewtonsoftJson扩展方法设置ASP.NET Core项目使用Newtonsoft.Json包.

And call AddNewtonsoftJson extension method to set ASP.NET Core project to use Newtonsoft.Json packages.

services.AddControllers().AddNewtonsoftJson();

相关链接

使用 Newtonsoft.Json.NET Core 3+ 项目

这篇关于当我使用下划线符号时,属性 JsonProperty 在 .NET Core 3.1 中工作不正确的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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