I am using the WCF REST 40 (CS) service template. I am throwing WebFaultExceptions as such:
throw new WebFaultException<string>("Error Message", HttpStatusCode.BadRequest);
However, when I test this with my client, everything returns as an Http 500 status code and the response is XML. I see an error message in the XML response. When I make the call correctly, I get a 200 response, and the answer is in JSON, which is correct, given the configuration of my configuration and ServiceContract.
The only way I can get the HTTP status code: 400 for Bad Request is to do this:
WebOperationContext.Current.OutgoingResponse.StatusCode = HttpStatusCode.BadRequest;
I still cannot get an exception to return as JSON.
Edit Adding a signature for more information:
[OperationContract] [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat = WebMessageFormat.Json, UriTemplate = "myendpoint")]
Is there an easy way to do this?
source share