0

In my ASP.Net REST controller I do the following:

return BadRequest("A problem happened!");

When calling this over HTTP I get the message as follows:

HttpResponseMessage response = await _httpClient.GetAsync(url);
if (!response.IsSuccessStatusCode)
{
    var errorMessage = await response.Content.ReadAsStringAsync();

But the value of the errorMessage is:

"\"A problem happened!\""

When I run through PostMan it doesn't include the quote marks even in the raw output view so I'm not sure if they are being added on for some reason, but I would rather avoid having to manually strip them.

I based my work on this other question where nobody seemed to have this issue: BadRequest custom error message not returned to client?

Mr. Boy
  • 60,845
  • 93
  • 320
  • 589

1 Answers1

0

It is just a json string, you have to deserialize it

 var errorMessage = "\"A problem happened!\""; // "A problem happened!"
 var errMsg = JsonConvert.DeserializeObject<string>(errorMessage); // A problem happened!
Serge
  • 40,935
  • 4
  • 18
  • 45