WebApi's {"message":"an error has occurred"} on IIS7, not in IIS Express
The problem was a missing dependency that wasn't on the server but was on my local machine. In our case, it was a Devart.Data.Linq dll.
To get to that answer, I turned on IIS tracing for 500 errors. That gave a little bit of information, but the really helpful thing was in the web.config setting the <system.web><customErrors mode="Off"/></system.web>
This pointed to a missing dynamically-loaded dependency. After adding this dependency and telling it to be copied locally, the server started working.
Basically:
Use IncludeErrorDetailPolicy
instead if CustomErrors
doesn't solve it for you (e.g. if you're ASP.NET stack is >2012):
GlobalConfiguration.Configuration.IncludeErrorDetailPolicy
= IncludeErrorDetailPolicy.Always;
Note: Be careful returning detailed error info can reveal sensitive information to 'hackers'. See Simon's comment on this answer below.
TL;DR version
For me CustomErrors
didn't really help. It was already set to Off
, but I still only got a measly an error has occurred
message. I guess the accepted answer is from 3 years ago which is a long time in the web word nowadays. I'm using Web API 2 and ASP.NET 5 (MVC 5) and Microsoft has moved away from an IIS-only strategy, while CustomErrors
is old skool IIS ;).
Anyway, I had an issue on production that I didn't have locally. And then found I couldn't see the errors in Chrome's Network tab like I could on my dev machine. In the end I managed to solve it by installing Chrome on my production server and then browsing to the app there on the server itself (e.g. on 'localhost'). Then more detailed errors appeared with stack traces and all.
Only afterwards I found this article from Jimmy Bogard (Note: Jimmy is mr. AutoMapper!). The funny thing is that his article is also from 2012, but in it he already explains that CustomErrors
doesn't help for this anymore, but that you CAN change the 'Error detail' by setting a different IncludeErrorDetailPolicy
in the global WebApi configuration (e.g. WebApiConfig.cs
):
GlobalConfiguration.Configuration.IncludeErrorDetailPolicy
= IncludeErrorDetailPolicy.Always;
Luckily he also explains how to set it up that webapi (2) DOES listen to your CustomErrors
settings. That's a pretty sensible approach, and this allows you to go back to 2012 :P.
Note: The default value is 'LocalOnly', which explains why I was able to solve the problem the way I described, before finding this post. But I understand that not everybody can just remote to production and startup a browser (I know I mostly couldn't until I decided to go freelance AND DevOps).
None of the other answers worked for me.
This did: (in Startup.cs)
public class Startup
{
public void Configuration(IAppBuilder app)
{
var config = new HttpConfiguration();
WebApiConfig.Register(config);
// Here:
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
}
}
(or you can put it in WebApiConfig.cs):
public static class WebApiConfig
{
public static void Register(HttpConfiguration config)
{
// Web API routes
config.MapHttpAttributeRoutes();
config.Routes.MapHttpRoute(
name: "DefaultApi",
routeTemplate: "api/{controller}/{action}/{id}",
defaults: new { id = RouteParameter.Optional }
);
// Here:
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
}
}