c# - Error for PUT,POST and DELETE RestAPI -


only method working time getting error put,post , delete. tried updating handler mapping thru web.config under iis site too. getting error status code 405 method not allowed. when changed handler mapping

  <system.webserver>   <handlers>    <remove name="extensionlessurlhandler-integrated-4.0" />    <remove name="optionsverbhandler" />    <remove name="traceverbhandler" />    <remove name="webdav" />    <remove name="extensionlessurlhandler-integrated-4.0" />    <add name="extensionlessurlhandler-integrated-4.0" path="*." verb="*" type="system.web.handlers.transferrequesthandler" precondition="integratedmode,runtimeversionv4.0" />     <remove name="extensionlessurl-integrated-4.0" />    <add name="extensionlessurl-integrated-4.0"        path="*."        verb="get,head,post,debug,delete,put"        type="system.web.handlers.transferrequesthandler"        precondition="integratedmode,runtimeversionv4.0" /> </handlers>  <validation validateintegratedmodeconfiguration="false" />  <modules>   <remove name="applicationinsightswebtracking" />   <add name="applicationinsightswebtracking" type="microsoft.applicationinsights.web.applicationinsightshttpmodule, microsoft.ai.web" precondition="managedhandler" />   <remove name="webdavmodule"/> </modules> 

started getting error 415 "unsupported media type".following repsonce getting

{statuscode: 415, reasonphrase: 'unsupported media type', version: 1.1, content: system.net.http.streamcontent, headers: { cache-control: no-cache pragma: no-cache server: microsoft-iis/8.5 x-aspnet-version: 4.0.30319 x-powered-by: asp.net date: thu, 17 nov 2016 16:44:52 gmt content-type: application/octet-stream; charset=utf-8 expires: -1 content-length: 100 }}

. following apicalls

    // put: api/creditrequests/5     [responsetype(typeof(void))]     public ihttpactionresult putcreditrequest(guid id, creditrequest creditrequest)     {         if (!modelstate.isvalid)         {             return badrequest(modelstate);         }          if (id != creditrequest.creditrequestid)         {             return badrequest();         }          db.entry(creditrequest).state = entitystate.modified;          try         {             db.savechanges();         }         catch (dbupdateconcurrencyexception)         {             if (!creditrequestexists(id))             {                 return notfound();             }             else             {                 throw;             }         }          return statuscode(httpstatuscode.nocontent);     }      // post: api/creditrequests     [responsetype(typeof(creditrequest))]     public ihttpactionresult postcreditrequest(creditrequest creditrequest)     {         if (!modelstate.isvalid)         {             return badrequest(modelstate);         }          db.creditrequests.add(creditrequest);          try         {             db.savechanges();         }         catch (dbupdateexception)         {             if (creditrequestexists(creditrequest.creditrequestid))             {                 return conflict();             }             else             {                 throw;             }         }          return createdatroute("defaultapi", new { id = creditrequest.creditrequestid }, creditrequest);     }      // delete: api/creditrequests/5     [responsetype(typeof(creditrequest))]     public ihttpactionresult deletecreditrequest(guid id)     {         creditrequest creditrequest = db.creditrequests.find(id);         if (creditrequest == null)         {             return notfound();         }          db.creditrequests.remove(creditrequest);         db.savechanges();          return ok(creditrequest);     } 

and calling them using httpclient object. code

  string jsondata = jsonconvert.serializeobject(item);             var content = new stringcontent(jsondata, system.text.encoding.utf8, "application/json");             httpresponsemessage response = null;             using (var client = getformattedhttpclient())// adding basic authentication in httpclientobject before using it.             {               if (isnew == true)                     response = client.postasync (_weburi, content).result;                 else if (isnew == false)                     response = client.putasync(_weburi, content).result;              }             if (!response.issuccessstatuscode)                                return false;             else             return true; 

this works me.

protected async task<string> connect(string url,wsmethod method,stringcontent body) {         try         {              httpclient client = new httpclient             {                 timeout = timespan.fromseconds(variables.serverwaitingtime)             };             if (await controller.getisinternetaccessavailable())             {                 if (variables.currentuser != null)                 {                     var authdata = string.format("{0}:{1}:{2}", variables.currentuser.login, variables.currentuser.mdp, variables.token);                     var authheadervalue = convert.tobase64string(encoding.utf8.getbytes(authdata));                      client.defaultrequestheaders.authorization = new authenticationheadervalue("basic", authheadervalue);                 }                  httpresponsemessage response = null;                 if (method == wsmethod.put)                     response = await client.putasync(url, body);                 else if (method == wsmethod.post)                     response = await client.postasync(url, body);                 else                     response = await client.getasync(url);                  .... } 

Comments

Popular posts from this blog

account - Script error login visual studio DefaultLogin_PCore.js -

xcode - CocoaPod Storyboard error: -