36

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):

enter image description here

What I get (in Web API):

enter image description here 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?

G. Stoynev
  • 7,389
  • 6
  • 38
  • 49
Martin Hansen
  • 5,154
  • 3
  • 32
  • 53
  • I dont get that screen. Are you using .NET 4.5? – Aliostad Apr 20 '12 at 13:53
  • Yes I am using 4.5 and EF 5 prerelease. – Martin Hansen Apr 20 '12 at 14:20
  • 11
    The main question is right at the end: How to make the error surface in the browser/client? I found this: http://lostechies.com/jimmybogard/2012/04/18/custom-errors-and-error-detail-policy-in-asp-net-web-api/ You need to do GlobalConfiguration.Configuration.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always; – Martin Hansen Apr 20 '12 at 15:16
  • 6
    There's an issue in web API to address this: http://aspnetwebstack.codeplex.com/workitem/79 And a patch to WebApiContrib to do what my post did: https://github.com/WebApiContrib/WebAPIContrib/commit/27381146d65b3958cf267e7bb7fcd80e1b913b66 – Jimmy Bogard Apr 20 '12 at 15:43
  • This is indeed a real question. Voting up the question. – Karthik Murugesan Jul 23 '12 at 17:39
  • 1
    API Controllers use a different exception filtering system. I found this while trying to figure out why Elmah wasn't logging anything. This article explains nicely how to integrate with Elmah, @JimmyBogard and Martin-hansen seemed to have figured the actual problem out. http://www.tugberkugurlu.com/archive/asp-net-web-api-and-elmah-integration – scaryman Aug 14 '12 at 20:29

2 Answers2

2

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

Nikola Markezic
  • 329
  • 1
  • 5
1

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.

Gaurav Karwal
  • 116
  • 2
  • 10