i have web api project returns json objects. objects populated correctly, when received calling application, empty. suspect json being denied requests.
my api controller code looks this:
public ihttpactionresult getthing(string id) { try { var model = new thing(id); var res = json<thing>(model); return res; } catch { return notfound(); } }
at point "res" correctly populated. on other (client) side have:
internal static jsonresult getthing(string thingid) { return gettask<jsonresult>(string.format(thingurl, thingid)); }
when check value of jsonobject here, it's empty (data null), notice value of field "jsonrequestbehavior" "denyget" suspect issue.
my question is, how set value of "allowget", can use populated object? i'm losing little hair have left!
you not need convert object json in controller. should able have controller code this:
public thing get(string id) { try { var model = new thing(id); return model; } catch { //throw not found exception } }
then when make request, ensure accept: application/json header set , should golden.
edit: further comment, , code making request not visible, can assume code have written call api endpoint must incorrect.
it should this:
using (var client = new httpclient()) { client.baseaddress = new uri("baseurlofapi"); client.defaultrequestheaders.accept.clear(); client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/json")); httpresponsemessage response = await client.getasync("realtiveurltocontroller/thingid"); if (response.issuccessstatuscode) { thing thing = await response.content.readasasync<thing>(); // whatever want thing. } }
more information on can found here
Comments
Post a Comment