How to force WCF return a fault exception as JSON

WCF

Suppose we use a webHttpBinding and in order to return a custom status code with some details, we should throw a WebHttpException<DetailsObject>, the problem is that by default WCF throws it as an XML instead of JSON, even if the response type is set to JSON in WebInvoke. To fix that, edit web.config and set the web behavior to the following, although counterintuitive, it actually works.


<endpointBehaviors>
        <behavior name="webHttp">
          <webHttp defaultBodyStyle="Bare" defaultOutgoingResponseFormat="Json" automaticFormatSelectionEnabled="false" 
			faultExceptionEnabled="false" helpEnabled="true" />
    </behavior>
</endpointBehaviors>

Post a Comment

Previous Post Next Post