var response = new HttpResponseMessage(HttpStatusCode.BadRequest)
{
Content = new StringContent("Error in Validation"),
ReasonPhrase = "Error in Validation"
};
throw new HttpResponseException(response);
The above code is not returning "Error in Validation" as response Content instead returns "Bad Request".
string result = response.Content.ReadAsStringAsync().Result;
I can read it from the ReasonPhrase but some customers want the error message in the response body. If HTTP Status is set as OK then it will return the correct message.
Was able to fix it, in case someone has same issue. I have coexisting website and API and the problem was due to custom error page forwarding.
I have to add below section to the Web.config to solve the problem.
You might consider using HttpRequestMessageExtensions.CreateErrorReponse from the System.Net.Http namespace.
There is a discussion of a similar problem here.