当我使用下划线符号时,属性 JsonProperty 在 .NET Core 3.1 中工作不正确

Attribute JsonProperty works incorrect with .NET Core 3.1 when I use underscore symbol

我有以下 JSON 补丁请求:

{
    "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"
}

以及以下映射模型:

    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)
{
    ...
}

发送此请求后,我得到以下信息:

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

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

app.UseRouting();

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

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

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

Install-Package Microsoft.AspNetCore.Mvc.NewtonsoftJson

并调用 AddNewtonsoftJson 扩展方法来设置 ASP.NET 核心项目使用 Newtonsoft.Json 包。

services.AddControllers().AddNewtonsoftJson();

相关链接

Using Newtonsoft.Json In .NET Core 3+ Projects

如果你想为 .net core 3.1

使用本机 System.Text.Json,你也可以使用 [JsonPropertyName("model")] 属性而不是 [JsonPropertyAttribute("model")]