Asp.net Web API returns non-descriptive error 500

僤鯓⒐⒋嵵緔 提交于 2019-12-09 04:39:13

问题


In my setup I get error 500 if anything goes wrong with my Web API request.

For instance with this simple code.

public IQueryable<Article> Get(){
    throw new Exception("error");
    return db.Articles; //yeah i know.. unreachable, not the point
}

What I expect(and what happens in a regular MVC controller):

What I get (in Web API):

My web config:
<customErrors mode="Off"/>
<compilation debug="true" targetFramework="4.5">

//under webserver
<httpErrors errorMode="detailed"/>

The app pool is running 4.0 in integrated mode. 32-bit applications are enabled.

How can I get the error to surface in the browser? Or at the very least during debugging?


回答1:


Had the same problem.

For me adding the

    GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;

in the WebApiConfig Register method helped.

After that I got nice detailed info about the error.

Here is some more info on MSDN




回答2:


If it is a WCF webservice, you will have to turn the IncludeExceptionDetailInFaults flag of the ServiceBehavior attribute to true for the error to appear the way you want.



来源:https://stackoverflow.com/questions/10243754/asp-net-web-api-returns-non-descriptive-error-500

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!