I am using HttpClient to call my MVC 4 web api. In my Web API call, it returns a domain object. If something goes wrong, an HttpResponseException with a configured message will be thrown on the server.
[System.Web.Http.HttpGet] public Person Person(string loginName) { Person person = _profileRepository.GetPersonByEmail(loginName); if (person == null) throw new HttpResponseException( Request.CreateResponse(HttpStatusCode.NotFound, "Person not found by this id: " + id.ToString())); return person; }
I see a customized error message in the response body using IE F12. However, when I call it using HttpClient , I do not receive a customized error message, but only an http code. "ReasonPhrase" is always "not found" for 404 or "Internal server error" for 500 codes.
Any ideas? How to send back a custom error message from the web API, and keep the normal return type as an object of my domain?
source share