我將JSON發佈到WebAPI控制器,但模型上的屬性未被綁定。WebAPI POST [FromBody] not binding
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
作爲參數,並手動綁定它,但感覺上面應該只是工作(tm)?
更新:
我已經改變了返回模型的方法,而我仍然收到空。
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
你可以顯示用戶的類定義嗎? –
#facepalm。沒有參數的構造函數。爲有見識的例外歡呼.net! – mattdwen
有沒有這樣的事情,羞辱刪除一個問題? – mattdwen