asp.net - Cannot post object from .net Client web-api web service -


i have 1 .net client tries make http request web api service here request:

   public list<category> getcategories()         {           httpclient client = new httpclient();           client.baseaddress = new uri("http://localhost:54558/");         client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/json"));          task<string> response = client.getstringasync("api/categoryapi/");         list<category> lstcategory = jsonconvert.deserializeobjectasync<list<category>>(response.result).result;         return lstcategory;     }        public void create(category category)     {         client.defaultrequestheaders.accept.add(new mediatypewithqualityheadervalue("application/json"));         var stringcontent = new stringcontent(category.tostring());         httpresponsemessage responsemessage = client.postasync("api/categoryapi/", stringcontent).result;      } 

and in webapi

 public ienumerable<category> getcategories()         {             return categoryrepository.data;         }            public string postcategory(category category)         {             categoryrepository.add(category);             return "messageok";         } 

so when make request getcategories action of web-api ok.
, no matter seems .net application cannot find post action of web-api , never see entering in postcategory method have put breakpoints here.

i error

atuscode: 500, reasonphrase: 'internal server error', version: 1.1, content: system.net.http.streamcontent, headers: {   pragma: no-cache   x-sourcefiles: =?utf-8?b?qzpcvxnlcnnccg9zdgdyzxncrgvza3rvcfxxzwjbcgltzxj2awnlxfdlykfwavnlcnzpy2vcyxbpxenhdgvnb3j5qxbpxa==?=   cache-control: no-cache   date: sat, 13 jun 2015 17:55:16 gmt   server: microsoft-iis/8.0   x-aspnet-version: 4.0.30319   x-powered-by: asp.net   content-length: 1022   content-type: application/json; charset=utf-8   expires: -1 }} 

what may issue. thak in advance

you posting category.tostring if didn't override tostring json or xml string, fail on server side because there no way deserialize content category object. should serialize category on client before posting it. make sure request headers include proper content-type of application/json. posting stringcontent, content-type won't application/json. setting accept header, describes data coming client, not data posting. 1 last thing, not use same httpclient both , post request. each method should use it's own httpclient don't have headers depending on call.


Comments

Popular posts from this blog

c# - Validate object ID from GET to POST -

node.js - Custom Model Validator SailsJS -

php - Find a regex to take part of Email -