WebAPI POST 没有绑定
我将JSON发布到WebAPI控制器,但模型上的属性未受约束。
public void Post([FromBody] Models.Users.User model) { throw new Exception(model.Id.ToString()); }
原始请求如下:
POST http://diva2.local/siteapi/User HTTP/: diva2.local Connection: keep-alive Content-Length:: application/json, text/plain, */* Origin: http://diva2.local X-Requested-With: XMLHttpRequest User-Agent: Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.31 (KHTML, like Gecko) Chrome/26.0.: application/json;charset=UTF: http://diva2.local/Users Accept-Encoding: gzip,deflate,sdch Accept-Language: en-US,en;q=: ISO-8859-1,utf-8;q=0.7,*;q=: .ASPXAUTH=4; __RequestVerificationToken=Rp_hUysjwCjmsxw2 {"Id":3,"FirstName":"DIVA2","LastName":"User1","Username":"diva1","IsApproved":true,"IsOnlineNow":true,"IsChecked":true}
我能找到的每个例子都告诉我这应该有效,但是model.Id == null
。
但是,如果我将JSON更改为:
{User: {"Id":3,"FirstName":"DIVA2","LastName":"User1","Username":"diva1","IsApproved":true,"IsOnlineNow":true,"IsChecked":true}}
一切都正确绑定。
这似乎不正确。 我想我可以接受JObject
作为参数,并手动绑定它,但感觉上面应该Just Work(tm)?
更新:
我已经改变了返回模型的方法,但仍然收到null。
public Models.Users.User Post(Models.Users.User user) { return user; }
并回应:
HTTP/1.1 200 OK Cache-Control: no-cache Pragma: no-cache Content-Type: application/json; charset=utf-8 Expires: -1 Server: Microsoft-IIS/8.0 X-AspNet-Version: 4.0.30319 X-MiniProfiler-Ids: ["a0fab514-d725-4d8f-9021-4931dc06ec4a","fdeeb9a8-9e36-41d8-91d3-5348e880e193","c1b4cc86-d7c3-4497-8699-baac9fa79bf1"] X-Powered-By: ASP.NET Date: Tue, 21 May 2013 09:06:00 GMT Content-Length: 4 null
您缺少Request中的Content-Type标头。
不幸的是,即使你检查过ModelState,我们也不会抛出任何错误信息。 但是,好消息是我们即将发布的版本已修复此行为,您将看到基于状态代码的415响应。
Web API要求Content-Type标头找出正确的格式化程序,以将主体反序列化为操作上的参数。
以下对我来说非常好:
模型:
public class User { public int Id { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Username { get; set; } public bool IsApproved { get; set; } public bool IsOnlineNow { get; set; } public bool IsChecked { get; set; } }
控制器:
public class ValuesController : ApiController { public User Post(User user) { return user; } }
请求:
POST http://example.com/api/values HTTP/1.1 Connection: keep-alive Accept: application/json, text/plain, */* Content-Type: application/json;charset=UTF-8 Host: localhost:8816 Content-Length: 125 {"Id":3,"FirstName":"DIVA2","LastName":"User1","Username":"diva2user1","IsApproved":true,"IsOnlineNow":true,"IsChecked":true}
响应:
HTTP/1.1 200 OK Cache-Control: no-cache Pragma: no-cache Content-Type: application/json; charset=utf-8 Expires: -1 Server: Microsoft-IIS/8.0 Date: Tue, 21 May 2013 08:59:02 GMT Content-Length: 125 {"Id":3,"FirstName":"DIVA2","LastName":"User1","Username":"diva2user1","IsApproved":true,"IsOnlineNow":true,"IsChecked":true}
正如您所看到的,在请求JSON有效负载中没有User
前缀的情况下,所有内容都很好并且花花公子。
小心model.Id
,因为如果你在路由定义中使用id
作为路由的一部分, id
可能具有特殊含义。 不要混淆两件事(路线参数和来自请求体有效载荷的参数)。
我的用户模型没有无参数构造函数。
我发现我的class级在所有属性上都有内部设置器。
改变public
public
到public
public
解决了我的问题