我的表格发布到WebApi有什么问题?

时间:2015-11-12 05:00:48

标签: asp.net-mvc asp.net-web-api asp.net-mvc-5

这是我试图调用的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; }
}

2 个答案:

答案 0 :(得分:0)

您看到的例外是一般情况,可能由多种因素引起。

但我认为你有循环引用错误。为了解决它, 将以下行放在Application_Start方法的顶部:

GlobalConfiguration.Configuration.Formatters.JsonFormatter.SerializerSettings.ReferenceLoopHandling = Newtonsoft.Json.ReferenceLoopHandling.Ignore;
GlobalConfiguration.Configuration.Formatters.Remove(GlobalConfiguration.Configuration.Formatters.XmlFormatter);

在这种情况下,它只强制返回JSON。

答案 1 :(得分:0)

尝试在模型上下文中将ProxyCreationEnabled设置为false

Configuration.ProxyCreationEnabled = false;

我的例子:

public partial class EventsEntities : DbContext
{
    public EventsEntities()
       : base("name=EventsEntities")
    {
        Configuration.ProxyCreationEnabled = false;
    }

    protected override void OnModelCreating(DbModelBuilder modelBuilder)
    {
        throw new UnintentionalCodeFirstException();
    }
}