I am trying to use the ModelState object to send Errors to the client; I am using asp.net API for a service.
On the web service side, I do this.
public HttpResponseMessage VerifyData(Cobject data) { string[] errors; if (!VerifyAllRequiredData(data, out errors)) { foreach(string error in errors) ModelState.AddModelError("", error); return Request.CreateErrorResponse(HttpStatusCode.ExpectationFailed, ModelState); } return Request.CreateResponse(HttpStatusCode.OK, data); }
I am creating a .NET Client library for this service, so we can use it for exsiting Windows applications.
on the client side:
public bool VerifyData(Cobject data) { try { HttpClient c = new HttpClient(); c.BaseAddress = BaseAddress; c.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(JsonHeader)); c.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(HTMLHeader)); c.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue(TextHeader)); var asyncResponse = this.PostAsJsonAsync(url, data); asyncResponse.Wait(); asyncResponse.Result.EnsureSuccessStatusCode(); return true; } catch (HttpRequestException hre) { Console.WriteLine(hre.Message); return false; } catch (Exception e) { Console.WriteLine(e.Message); return false; } }
The problem here is that I do not see the messages that were added to the ModelState on the repository. I see the status (ExpectionFailed), but no messages. How to receive these messages on the client?
source share