[英]How do I determine what is the issue with a 500 error on a HttpClient.PostAsJsonAsync call?
I am making a call to a Web API that I wrote. 我正在调用我编写的Web API。 I am working through the bugs on both sides and am getting a 500 error.
我正在解决双方的错误,并收到500错误。 I want to see that is in that error message to see what might be the problem.
我想看到那个错误消息,看看可能是什么问题。 How do I 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 where that text might be stored so I can figure out what other bugs need to be fixed. 我没有看到该文本可能存储在哪里,因此我可以找出需要修复的其他错误。 How do I get that text?
我如何获得该文本?
Update : something I didn't clarify. 更新 :我没有澄清的事情。 I put a breakpoint on the WebAPI I am calling and the break point is never hit.
我在我调用的WebAPI上放了一个断点,断点永远不会被击中。 However , when I call it from Poster, I hit the break point.
然而 ,当我从海报中调用它时,我达到了断点。 The URL is correct.
网址是正确的。
public HttpResponseMessage Post([FromBody]LeaveRequest value)
{
if (ModelState.IsValid)
{
// code here
}
}
I was able to make a change to get the error message: 我能够进行更改以获取错误消息:
var repsonse = client.PostAsJsonAsync(fooURL, leaveRequest).Result.Content.ReadAsStringAsync();
instead of 代替
var repsonse = client.PostAsJsonAsync(fooURL, leaveRequest).Result;
I was then able to see my error and fix it. 然后我能够看到我的错误并修复它。
I am making a call to a Web API that I wrote.
我正在调用我编写的Web API。 I want to see that is in that error message to see what might be the problem.
我想看到那个错误消息,看看可能是什么问题。
You might want to put the code you have in PayrollApi.LeaveRequest
in a try-catch block, something like: 您可能希望将
PayrollApi.LeaveRequest
中的代码放在try-catch块中,例如:
public HttpResponseMessage LeaveRequest()
{
try {
// do something here
}
catch(Exception ex) {
// this try-catch block can be temporary
// just to catch that 500-error (or any unexpected error)
// you would not normally have this code-block
var badResponse = request.CreateResponse(HttpStatusCode.BadRequest);
badResponse.ReasonPhrase = "include ex.StackTrace here just for debugging";
}
}
Then make a call inside a try-catch block to capture that extra error: 然后在try-catch块内调用以捕获额外的错误:
using (var client = new HttpClient())
{
// rest of your code goes here
try
{
var repsonse = client.PostAsJsonAsync(fooURL, leaveRequest).Result;
}
catch(HttpRequestException httpEx)
{
// determine error here by inspecting httpEx.Message
}
}
httpEx.Message
can have this message: httpEx.Message
可以有这样的消息:
Response status code does not indicate success: 500 ({stack_trace_goes_here}).
响应状态代码不表示成功:500({stack_trace_goes_here})。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.