JSON值无法转换为System.Nullable [System.Int32] [英] The JSON value could not be converted to System.Nullable[System.Int32]
问题描述
我将ASP.NET Core 2.2 API更新为ASP.NET Core 3.0,并且正在使用System.Json:
I updated an ASP.NET Core 2.2 API to ASP.NET Core 3.0 and I am using System.Json:
services
.AddMvc()
.SetCompatibilityVersion(CompatibilityVersion.Version_3_0)
.AddJsonOptions(x => {})
然后,我尝试使用以前使用过的Angular 8发布JSON数据:
I then tried to post JSON data using Angular 8, which was working before:
{
"name": "John"
"userId": "1"
}
ASP.NET Core 3.0 API中的模型是:
The model in the ASP.NET Core 3.0 API is:
public class UserModel {
public String Name { get; set; }
public Int32? UserId { get; set; }
}
API Controller的操作如下:
And the API Controller action is as follows:
[HttpPost("users")]
public async Task<IActionResult> Create([FromBody]PostModel) {
}
提交模型时,出现以下错误:
When I submit the model I get the following error:
The JSON value could not be converted to System.Nullable[System.Int32].
使用System.Json代替Newtonsoft时是否需要做其他事情?
Do I need to do something else when using System.Json instead of Newtonsoft?
推荐答案
在这里,通过json,您传递了UserId的字符串值,但是您的模型引用了int32?UserId的值.那么您的值如何从字符串转换为int32?
Here Via the json you pass a string value for UserId but your model refer a int32? value for UserId. Then how your value convert from string to int32?
这篇关于JSON值无法转换为System.Nullable [System.Int32]的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!