c# - WebAPI POST [FromBody] not binding -
i'm posting json webapi controller, properties on model not being bound.
public void post([frombody] models.users.user model) { throw new exception(model.id.tostring()); }
the raw request follows:
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, 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}
every example can find tells me should work, model.id == null
.
however, if change json to:
{user: {"id":3,"firstname":"diva2","lastname":"user1","username":"diva1","isapproved":true,"isonlinenow":true,"ischecked":true}}
everything binds correctly.
this doesn't seem correct. guess accept jobject
parameter, , bind manually, feels above should work (tm)?
update:
i've changed method return model, , still receive null.
public models.users.user post(models.users.user user) { return user; }
and response:
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
you missing content-type header in request.
unfortunately if had checked modelstate, not throwing error information. however, news behavior has been fixed our coming release , see 415 status code based response.
web api requires content-type header find out right formatter deserialize body parameter on action.
Comments
Post a Comment