ASP.NET Web API: Non-descriptive 500 Internal Server Error
Solution 1:
You can try adding:
GlobalConfiguration.Configuration.IncludeErrorDetailPolicy =
IncludeErrorDetailPolicy.Always;
to your Application_Start()
in the Global.asax. This solution works for many of the common errors.
If, however, you aren't getting satisfactory information you should consider writing an l Exception Filter and registering it globally.
This article should get you started. The core of what you need is to write & register something like:
public class NotImplExceptionFilter : ExceptionFilterAttribute {
public override void OnException(HttpActionExecutedContext context) {
if (context.Exception is NotImplementedException) {
context.Response = new HttpResponseMessage(HttpStatusCode.NotImplemented);
}
}
}
Solution 2:
Post RC, this issue was fixed and you will be getting error details also apart from the 500 Internal Server Error. (This issue is fixed for Web Host scenarios only though).
You could do the following to get the details of the actual exception which might be occurring during a formatter's WriteToStream method.
ObjectContent<IEnumerable<Product>> responseContent = new ObjectContent<IEnumerable<Product>>(db.Products.Include(p => p.ProductSubcategory).AsEnumerable(), new XmlMediaTypeFormatter()); // change the formatters accordingly
MemoryStream ms = new MemoryStream();
// This line would cause the formatter's WriteToStream method to be invoked.
// Any exceptions during WriteToStream would be thrown as part of this call
responseContent.CopyToAsync(ms).Wait();
Solution 3:
I ran into this same issue. I found Kiran Challa's response helpful in getting the actual exception being thrown outside of my action.
To solve my issue, setting the ProxyCreationEnabled property of my context to false got me a step further.
In my scenario, my next exception was due to a circular reference in my models. After cleaning that up, the phantom 500 response was gone. Good luck if you haven't solved this yet!