简体   繁体   English

当来自CreateErrorResponse时,HttpResponseMessage.Content.ReadAsStringAsync不反序列化JSON

[英]HttpResponseMessage.Content.ReadAsStringAsync don't deserialize JSON when it come from CreateErrorResponse

if i return from my selfhosted webapi 如果我从我自己的主题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(); 如果我把它作为对象阅读..它没关系...但我无法执行object.ToString(); i get errors.. 我收到错误..

why? 为什么? and how to fix it? 以及如何解决它?

I found that there were extra '\\' and '"' in the returned JSON. 我发现返回的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. 更好的解决方案是在webapi方法中创建响应时解决问题。 Note the type in the CreateResponse method. 请注意CreateResponse方法中的类型。

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");

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 使用HttpResponseMessage.Content.ReadAsStringAsync()时没有得到内容的全部; - Not getting the entirety of the content when using HttpResponseMessage.Content.ReadAsStringAsync(); 如何返回任务 <T> 从HttpResponseMessage.Content.ReadAsStringAsync()。ContinueWith()方法? - How to return the Task<T> from HttpResponseMessage.Content.ReadAsStringAsync().ContinueWith() method? HttpResponseMessage.Content.ReadAsStreamAsync() 与 HttpResponseMessage.Content.ReadAsStringAsync() - HttpResponseMessage.Content.ReadAsStreamAsync() vs HttpResponseMessage.Content.ReadAsStringAsync() 如何从HttpResponseMessage反序列化protobuf内容 - How to deserialize protobuf content from HttpResponseMessage 将HttpResponseMessage.Content数据(ReadAsStringAsync)限制为特定的最大大小 - Limit HttpResponseMessage.Content data (ReadAsStringAsync) to specific maximum size 从ReadAsStreamAsync或ReadAsStringAsync反序列化为CRM实体 - Deserialize to a CRM Entity from ReadAsStreamAsync or ReadAsStringAsync HttpResponseMessage的内容为JSON - Content of HttpResponseMessage as JSON 反序列化 HttpResponseMessage - Deserialize HttpResponseMessage HttpClient GetAsync 和 ReadAsStringAsync 需要反序列化复杂 JSON 响应的一部分 - HttpClient GetAsync and ReadAsStringAsync needed to deserialize just part of a complex JSON response 状态码不成功时无法读取HttpResponseMessage内容 - Can't read HttpResponseMessage content when the status code is not success
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM