Web Api 2 - IncludeErrorDetailPolicy

Posted by ryansouthgate on 17 Jul 2015

I’m writing a series of WebApi 2 Services, and noticed in exception details (mainly stack traces) coming through on calls which were failing. All actions exposed within the WebApi Controllers have a try-catch, however this one wasn’t getting caught (note another way to not have try-catches all over the place is to use ExceptionFilters ). I noticed it was coming from our Unity IoC, specifically from when instantiating a constructor, which in turn was returning a massive stack trace to the client.

The Web Api documentation mentions IncludeErrorDetailPolicy, which can be set programmatically in the Global.asax/WebApiConfig.cs>Register method, however I wanted to be able to turn this on/off without needing to redeploy.

After looking at the source code (specifically this commit) I was able to construct this xml in the web.config which allows me to toggle errors going back to the client and not having to re-deploy.

<system.web>
   <httpRuntime targetFramework="4.5" />
   <compilation debug="true" targetFramework="4.5" /> 
   <!-- WebApi - IncludeErrorDetailPolicy settings --> 
   <!-- Mode = RemoteOnly WebApi: LocalOnly On WebApi: Never Off WebApi: Always --> 
   <customErrors mode="On"></customErrors>
</system.web>


comments powered by Disqus