c# - Passed Variables Null -


i building small web api syncing data , pulling down objects works great, pushing objects doesn't work no matter have tried.

edited reflect changes:

here controller:

[system.web.mvc.httppost]     public void updatetasks([frombody] string s)     {         console.writeline(s);     } 

here client code:

httpcontent c = new stringcontent("1234");         httpclient client = new httpclient();         c.headers.contenttype = new mediatypeheadervalue("application/json");         client.baseaddress = new uri("http://localhost/qaqc_syncwebservice/tasks/updatetasks/");         var resp = client.postasync(client.baseaddress, c).result; 

i can value though if put in uri, string content alone doesn't seem work.

try

[httpput] public void updatetasks([frombody]string s) {     console.writeline(s); } 

please note:

  1. [frombody] parameters must encoded =value final hurdle remaining web api requires pass [frombody] parameters in particular format. that’s reason why our value parameter null in previous example after decorated method’s parameter [frombody]. instead of standard key=value encoding client- , server-side frameworks expect, web api’s model binder expects find [frombody] values in post body without key name @ all. in other words, instead of key=value, it’s looking =value. part is, far, confusing part of sending primitive types web api post method. not bad once understand it, terribly unintuitive , not discoverable.

from http://encosia.com/using-jquery-to-post-frombody-parameters-to-web-api/


Comments

Popular posts from this blog

get url and add instance to a model with prefilled foreign key :django admin -

css - Make div keyboard-scrollable in jQuery Mobile? -

ruby on rails - Seeing duplicate requests handled with Unicorn -