如何在.Net Core Rest API中将Json对象发送到ViewModel

时间:2019-05-14 07:56:47

标签: json asp.net-core asp.net-core-webapi

我有一个初始化为dto的ViewModel。我想从邮递员发送带有其值的json对象,但c#对象不能接受值,并且设置为null。我该怎么办?

public class RegisterUserVm
{
   public UserDto User { get; set; }

   public RegisterUserVm()
   {
      User = new UserDto();
   }
} 

public class UserDto
{
   public int Id { get; set; }
   public string Firstname { get; set; }
   public string Lastname { get; set; }
   public string Email { get; set; }
   public string Telephone { get; set; }
   public string Password { get; set; }
}


[HttpPost]
[Route("RegisterUser")]
public async Task<ActionResult<UserDto>> RegisterUser([FromBody] RegisterUserVm vm)
{
   var registeredUser = await userBusiness.RegisterUser(new RegisterUserCommand
                {
                    User = vm.User
                });

                return registeredUser.Result;
}

1 个答案:

答案 0 :(得分:-1)

[HttpPost]
[Route("RegisterUser")]
public async Task<IActionResult> RegisterUser([FromBody] RegisterUserVm vm)
{
   var registeredUser = await userBusiness.RegisterUser(new RegisterUserCommand
                {
                    User = vm.User
                });

                var resultModel = registeredUser.Result.ToResultModel();
                return Ok(resultModel);

}