.NET WebAPI 正确抛出错误详细信息


try
{ ... } catch (Exception e) { //在webapi中要想抛出异常必须这样抛出,否则之抛出一个默认500的异常 var resp = new HttpResponseMessage(HttpStatusCode.InternalServerError) { Content = new StringContent(e.ToString()), ReasonPhrase = "error" }; throw new HttpResponseException(resp); }

写try…catch…捕捉action中的错误并抛出依然获取不到详细信息,得不到错误详细信息就没有方向对错误进行修改,然后就在网上搜索解决办法,发现WebAPI出现错误,只会抛出500,并不包含详细信息,只有抛出HttpResponseException,才能得到错误的详细信息,这样就好办了,代码如下:

原文地址:https://www.cnblogs.com/joeylee/p/3828345.html