As title says, I’ve got 500 Internal Server Error from GET request to an IQueryable action. The body of the error is empty. That error happens after my action returns result
Fredrik Normén wrote a great blog post called ASP.NET Web API Exception Handling about that topic. His solution uses custom exception classes and an exception filter attribute that can be applied to all ApiController
action methods.
I had the issue in RC when I was not specifying query parameters in the right order. For example, if you specify $skip=0
, it will get 500, but if you specify $orderby=xxx&skip=0
no error.
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();
A deceivingly simple routing weakness caused this problem in my case: There was another HttpPost with the same signature (not name) in my Api Controller. The default routing did not resolve the name differences, and the ServiceError 500 was the response it gave before either of the Api functions were reached. Solution: change the default routing or your signatures and try again.
Here is my RouteConfig.cs that works quite well for standard WebApi2 usage:
public class RouteConfig
{
public static void RegisterRoutes(RouteCollection routes)
{
routes.IgnoreRoute("{resource}.axd/{*pathInfo}");
// Default is required in any case.
routes.MapRoute(
name: "Default",
url: "{controller}/{action}/{id}",
defaults: new { controller = "Home", action = "Index", id = UrlParameter.Optional }
);
}
}
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);
}
}
}
This Scenario was caused for the following reasons
The problem occurred due to misformed Web.config. ( Multiple configSections)
Instead of creating roslyn folder inside bin folder, I had it created at the root. (Deployment location.)
The best way to diagnose this was, to put a simple HTML page at the application location and try to browse it. 500 Error description will be displayed on this html page.
And also don't forget to add
<customErrors mode="Off"></customErrors>
to Web.config