简体   繁体   English

HttpClient PostAsync到WebApi-返回500,但不会引发异常)

[英]HttpClient PostAsync to WebApi - returning 500, but no exception thrown)

I'm making a call to a web api service like this: 我正在拨打这样的网络API服务:

var response = client.PostAsync("http://localhost:8080/api/values", new FormUrlEncodedContent(new[] {new KeyValuePair<string, string>("a", "b"),}))
                     .Result;

Result is a 结果是

"500 internal server error" “500内部服务器错误”

No exception is thrown (even though I've set VS to Break when CLR exceptions are thrown) 没有抛出异常(即使我将VS设置为在抛出CLR异常时也要中断)

I there any way I can force VS to bubble up the underlying exception? 我有什么办法可以迫使VS冒泡底层异常?

There is no underlying exception, that's why you can't bubble it up. 没有潜在的例外,这就是为什么您不能冒出来。 However, if you want to create one you can call 但是,如果要创建一个,可以致电

 response.EnsureSuccessStatusCode();

If the response code is a 4xx or 5xx then it will throw. 如果响应代码是4xx或5xx,则它将抛出。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM