c# - How do I determine what is the issue with a 500 error on a HttpClient.PostAsJsonAsync call? -


i making call web api wrote. working through bugs on both sides , getting 500 error. want see in error message see might problem. how find that?

using (var client = new httpclient()) {     var foourl = url.routeurl("payrollapi", new { httproute = string.empty, controller = "leaverequest" }, request.url.scheme);     var repsonse = client.postasjsonasync(foourl, leaverequest).result; } 

i don't see text might stored can figure out other bugs need fixed. how text?

update: didn't clarify. put breakpoint on webapi calling , break point never hit. however, when call poster, hit break point. url correct.

public httpresponsemessage post([frombody]leaverequest value) {     if (modelstate.isvalid)     {         // code here     } } 

i making call web api wrote. want see in error message see might problem.

you might want put code have in payrollapi.leaverequest in try-catch block, like:

public httpresponsemessage leaverequest() {     try {         // here     }      catch(exception ex) {         // try-catch block can temporary          // catch 500-error (or unexpected error)         // not have code-block         var badresponse = request.createresponse(httpstatuscode.badrequest);         badresponse.reasonphrase = "include ex.stacktrace here debugging";     } } 

then make call inside try-catch block capture error:

using (var client = new httpclient()) {     // rest of code goes here     try     {         var repsonse = client.postasjsonasync(foourl, leaverequest).result;     }     catch(httprequestexception httpex)     {          // determine error here inspecting httpex.message              } } 

httpex.message can have message:

response status code not indicate success: 500 ({stack_trace_goes_here}).


Comments