Jan 26, 2012 at 10:04 AM
Edited Jan 26, 2012 at 10:09 AM
There's been discussion elsewhere that in a custom error handler which might start like this:
public class MyErrorHandler : HttpErrorHandler
protected override bool OnTryProvideResponse(Exception exception, ref HttpResponseMessage message)
... that the message is always empty. In fact, it's up to the error handler to populate it.
I noticed that if you do this and don't attach the request to the message, then the formatters will not honour the accept headers in the request and consequently WebApi defaults to XML formatting.
To fix this, you need to manually add the request into the response with a workaround as followes. Say you are creating an HttpResponseMessage in the message variable, then :
message.RequestMessage = OperationContext.Current.RequestContext.RequestMessage.ToHttpRequestMessage();