【发布时间】:2016-02-13 09:09:42
【问题描述】:
这是我尝试调用的 Web API 操作:
// POST api/Account/Register
[System.Web.Http.AllowAnonymous]
[System.Web.Http.Route("Register")]
[System.Web.Http.HttpPost]
public async Task<IHttpActionResult> Register([FromBody] RegisterBindingModel model)
{
if (!ModelState.IsValid)
{
return BadRequest(ModelState);
}
var user = new ApplicationUser() { UserName = model.Email, Email = model.Email };
IdentityResult result = await UserManager.CreateAsync(user, model.Password);
if (!result.Succeeded)
{
return GetErrorResult(result);
}
return Ok();
}
这是我在 UI 的姊妹项目中发布给它的表单:
<form action="http://localhost:53231/api/Account/Register" method="post">
<div class="form-group">
@Html.LabelFor(m => m.Email, new { @class = "col-md-2 control-label" })
@Html.TextBoxFor(m => m.Email, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.Password, new { @class = "col-md-2 control-label" })
@Html.PasswordFor(m => m.Password, new { @class = "form-control" })
</div>
<div class="form-group">
@Html.LabelFor(m => m.ConfirmPassword, new { @class = "col-md-2 control-label" })
@Html.PasswordFor(m => m.ConfirmPassword, new { @class = "form-control" })
</div>
<input type="submit" class="btn btn-default" value="Register" />
</form>
当我尝试提交表单时,我在 XML 响应中收到以下错误:
<ExceptionMessage>
The 'ObjectContent`1' type failed to serialize the response body for content type 'application/xml; charset=utf-8'.
</ExceptionMessage>
API 操作的第一行上的断点甚至都不会被命中。我做错了什么?
这是我期待的模型:
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; }
}
【问题讨论】:
-
RegisterBindingModel是否只包含这 3 个属性? -
@StephenMuecke 是的,我已将该模型添加到问题中。
标签: asp.net-mvc asp.net-web-api asp.net-mvc-5