I have a Web API controller POST method that behaves fine locally and on the testing server. If everything goes well it returns:
new HttpResponseMessage( Htt
OWIN
Since the host choice is not mentioned here, I just wanted to add that in case of OWIN you have to use syneptody's solution BUT not in Global.asax, as per this answer and my own tests.
You want to insert IncludeErrorDetailPolicy.Always
in the Startup.cs
file:
public void Configuration(IAppBuilder appBuilder)
{
HttpConfiguration Config = new HttpConfiguration();
Config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
...
}
It sounds like it could very well be your customErrors mode to me. WebAPI is running on top of ASP.NET (MVC), so it uses all the same web.config settings.
If it is a test server, you could try turning customErrors off to verify.
<system.web>
<customErrors mode="Off" />
</system.web>
Had the same issue. It's indeed because of the custom errors setting.
In a real world scenario, you would definitely want to use a custom error page in your application, but in order for custom exception messages to work in the WebAPI you need to disable the custom errors page.
How to fix this? Luckily, you can use the <location>
element in your web.config to solve this.
Solution:
<!-- General for the application -->
<system.web>
<customErrors mode="RemoteOnly" defaultRedirect="YourCustomErrorPage.aspx"/>
</system.web>
<!-- Override it for paths starting with api (your WebAPI) -->
<location path="api">
<system.web>
<customErrors mode="Off" />
</system.web>
</location>
I use this method in my own app, works well.
There have been many changes in the Web API code base since beta. Lots of awesomeness. See how to get nightly signed builds here.
The generic HttpResponseMessage<T>
is no longer supported. Use HttpRequestMessage.CreateResponse<T>
. See this and this.
You'll want to update to at least the current nightly build instead of using the beta if you're planning on sticking with it going forward. So many good improvements particularly for Web API.
EDIT: In my mind, this was actually related to your original question because, while I haven't look into it for a concrete answer, it seems the newer stuff returns responses that don't get intercepted by IIS. It may have to do with the re-working of the error handling / error reporting.
UPDATE 8/14/2012 The current Release Candidate for MVC 4 / Web API is good enough. You don't need to get the nightly build anymore unless you want to stay utterly up-to-date.
Take a look at this MSDN post on the HttpConfiguration.IncludesErrorDetailPolicy:
In your Global.asax:
var config = GlobalConfiguration.Configuration;
config.IncludeErrorDetailPolicy = IncludeErrorDetailPolicy.Always;
I have used this configuration property to force error messages to include details.