ASP.NET Web API 2模型没有绑定

时间:2014-03-09 15:40:58

标签: c# asp.net asp.net-mvc web-services

我正在使用VS 2013和ASP.NET Web API 2 作为iOS应用的后端。一切顺利,直到我尝试使用Web API 2实现我的第一个POST。我使用MVC Web API 1构建了一个以前的API,没有实现POST的问题。使用[FromBody]属性应该有效,但我的“请求”始终为空。该模型没有约束力。任何人都可以看到我的错误或给我一些帮助吗?

UsrDTO

public class UsrDTO
{
    public string firstName { get; set; }
    public string middleName { get; set; }
    public string lastName { get; set; }
    public string email { get; set; }
    public int companyId { get; set; }
    public bool isManager { get; set; }
    public bool isAdmin { get; set; }
}

Usr Controller

[RoutePrefix("api/usr")]
public class UsrController : BaseAPIController
{
    [Route("")]
    public HttpResponseMessage Post([FromBody]UsrDTO request)
    {
        var ctx = repo.GetContext();
        Usr user = new Usr();
        user.Id = new Guid(userId);
        user.Username = request.email;
        user.FirstName = request.firstName;
        user.MiddleName = request.middleName;
        user.LastName = request.lastName;
        user.Email = request.email;
        user.CompanyId = request.companyId;
        user.IsManager = request.isManager;
        user.IsAdmin = request.isAdmin;
        user.IsActive = true;
        user.CreatedOn = DateTime.Now;
        ctx.Usr.Add(user);
        ctx.SaveChanges();
        return Request.CreateResponse(HttpStatusCode.OK, user);
     } 
}

提琴手请求

POST http://localhost:49763/api/usr HTTP/1.1
User-Agent: Fiddler
Host: localhost:49763
Content-Length: 230
Content-Type: application/json; charset=utf-8

{
     "firstName" : "test",
     "middleName" : "test",
     "lastName":"test",
     "email": "test@test.com", 
     "companyId" = 1,
     "isManager"=false, 
     "isAdmin"=false
}

1 个答案:

答案 0 :(得分:-2)

呃,请求正文中我的json中的拼写错误。立即行动。