if i return from my selfhosted webapi
Request.CreateResponse(HttpStatusCode.OK, "YAY");
everything is fine.. so i can read it like that:
var responseStr = await Client.Content.ReadAsAsync<string>();
and then make something like "MessageBox.Show(responseStr);
if i return
Request.CreateErrorResponse(HttpStatusCode.NotFound, "something went wrong!");
and i read it out the same way or even with(doesn't matter how):
Client.Content.ReadAsStringAsync();
the string is not deserialized and i get an error when trying to parse / read as string.
if i read it as object .. it's fine.. but i can't perform object.ToString(); i get errors..
why? and how to fix it?
I found that there were extra '\\' and '"' in the returned JSON.
So before I serialize back to an object, I needed to remove the extra chars.
eg
string jsonString = httpResponseMessage.Content.ReadAsStringAsync()
.Result
.Replace("\\", "")
.Trim(new char[1] { '"' });
List<VwAisItemMaster> vwAisItemMasterList = JsonConvert.DeserializeObject<List<VwAisItemMaster>>(jsonString);
The better solution is just to fix the issue when creating the response in the webapi method. Note the type in the CreateResponse method.
IList<VwItemMaster> vwItemMasterList = this.itemMastersGetByUpc(unitOfWork, upc);
HttpResponseMessage httpResponseMessage = this.Request.CreateResponse<IList<VwItemMaster>>(HttpStatusCode.OK, vwItemMasterList);
httpResponseMessage.Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.