【问题标题】:Web API 2 parameter binding missing some valuesWeb API 2 参数绑定缺少一些值
【发布时间】:2016-02-26 17:21:33
【问题描述】:

我正在创建一个 Web API 2 应用程序和一个单独的 MVC 客户端,因为会有移动应用程序也访问 Web API 2 应用程序。

在 Web API 2 中,RegisterBindingModel 类是

public class RegisterBindingModel
{
    [Required]
    [Display(Name = "Email")]
    public string Email { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
}

在客户端,RegisterBinderModel 类是

public class RegisterBindingModel
{
    [Required]
    [EmailAddress]
    [Display(Name = "Email")]
    public string Email { get; set; }

    [Required]
    [StringLength(100, ErrorMessage = "The {0} must be at least {2} characters long.", MinimumLength = 6)]
    [DataType(DataType.Password)]
    [Display(Name = "Password")]
    public string Password { get; set; }

    [DataType(DataType.Password)]
    [Display(Name = "Confirm password")]
    [Compare("Password", ErrorMessage = "The password and confirmation password do not match.")]
    public string ConfirmPassword { get; set; }
}

在我的 MVC 客户端中,我正在尝试注册一个新用户。

    // POST: /Account/Register
    [HttpPost]
    [AllowAnonymous]
    [ValidateAntiForgeryToken]
    public async Task<ActionResult> Register(RegisterBindingModel model)
    {
        if (ModelState.IsValid)
        {
            var user = new ApplicationUser { UserName = model.Email, Email = model.Email };

            System.Diagnostics.Debug.Print(model.Email);
            System.Diagnostics.Debug.Print(model.Password);
            System.Diagnostics.Debug.Print(model.ConfirmPassword);
            System.Diagnostics.Debug.Print(url);

            HttpClient test = new HttpClient();

            HttpResponseMessage result2= await  test.PostAsJsonAsync(url, user);

注册后的方法是

    // POST api/Account/Register
    [AllowAnonymous]
    [Route("Register")]
    public async Task<IHttpActionResult> Register(RegisterBindingModel model)
    {
        System.Diagnostics.Debug.Print(model.Email);
        System.Diagnostics.Debug.Print(model.Password); // Is null?
        System.Diagnostics.Debug.Print(model.ConfirmPassword); //Is null?

        if (!ModelState.IsValid) // Is of course false
        {
            return BadRequest(ModelState);
        }

我遇到的问题是只有电子邮件值绑定在 Web API 注册方法中。 my post 方法的 bound 参数中的 password 和 confirmpassword 值为 null。任何想法为什么?

【问题讨论】:

  • 这是因为您发布了一个 ApplicationUser,它只有一个 Email 属性集。请尝试发布 model

标签: asp.net-web-api2


【解决方案1】:

这是因为您发布的是 user,其中一个 ApplicationUser & 只有其 Email 属性集:

var user = new ApplicationUser { UserName = model.Email, Email = model.Email };
HttpClient test = new HttpClient();
HttpResponseMessage result2 = await test.PostAsJsonAsync(url, user);

尝试改为发布model

HttpResponseMessage result2 = await test.PostAsJsonAsync(url, model);

【讨论】:

  • 我想删除这个愚蠢的问题,因为除了其他需要更多睡眠的人之外,它对其他人并没有真正的帮助......
  • @user3140169 哈哈。永远不要删除真相,以免忘记。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-11-14
相关资源
最近更新 更多