我使用react-redux模板创建了一个简单的.netcore 2.1应用程序,其中有一个用于注册用户的API。控制器如下。
[HttpPost("register")]
public async Task<ActionResult<string>> Register(User user)
{
await this._authService.Register(user);
return Ok();
}
当我尝试使用控制器测试此API时,用户的属性将返回为null。以下是用户模型和邮递员请求。
public class User : IEntitybase
{
[Key]
public string Id { get; set; }
[Required]
public string Name {get; set;}
[Required]
public string Email {get; set;}
[Required]
public string Password {get; set;}
}
}
由于发送用户作为帖子主体不起作用我尝试发送用户参数作为查询参数,即使我没有提到从查询参数获取参数,这令人惊讶地工作。
在此之后,我使用[FromBody]属性更改了API控制器,当我在正文中发送属性时,该属性仍然使用户为null。
这里的问题可能是它默认采用查询参数而不是从正文中获取?
尝试使用像Postman这样的API客户端。
请求应该是GET请求标头应该具有Content-Type作为“application / json”,它可以是其他东西,但尝试这个开始。将身体设为
{
"Id": "123",
"Name": "some name",
"Email": "[email protected]",
"Password": "somepwd"
}
如果您未在模型中使用[JsonProperty(PropertyName =“FooBar”)],则属性名称应与POST数据匹配(区分大小写)。
因此在你的场景中,在邮递员中,
{
"Id" : "555",
"Name" : "Some Name",
"Email" : "[email protected]",
"Password" : "Password"
}