[英]ReadAsAsync calling a WebAPI not reconstituting json to class
I am making a call to a WebAPI that returns some Content from a db via JSON.我正在调用一个 WebAPI,它通过 JSON 从数据库返回一些内容。 The content is visible in the JSON (viewed using Fiddler) but when I try to put the response data back into a class all the values are null in the class.
内容在 JSON 中可见(使用 Fiddler 查看)但是当我尝试将响应数据放回类中时,类中的所有值都为空。
This used to work but now fails.这曾经有效,但现在失败了。 I suspect that I may not be using the GetAsync and ReadAsAsync correctly.
我怀疑我可能没有正确使用 GetAsync 和 ReadAsAsync。
new note This is one example but there are several other similar calls that cause the same issue. new note这是一个示例,但还有其他几个类似的调用会导致相同的问题。
Any thoughts why this has stopped working?任何想法为什么这已经停止工作? and what would be the correct way to fix it?
修复它的正确方法是什么?
The Class:班上:
public class ContentElement : ICloneable
{
public object Clone()
{
return this.MemberwiseClone();
}
public int PageId { get; set; }
public string LanguageCode { get; set; }
public int ContentId { get; set; }
public string PageName { get; set; }
public string Content { get; set; }
public int? DisplayOrder { get; set; }
}
The Code:编码:
public static List<ContentElement> GetContent(int siteId)
{
var contentElements = new List<ContentElement>();
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(GlobalValues.APIUri);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
AddAuthenticationHeader(client);
var result = client.GetAsync("api/Content/GetContent/" + (object)siteId).Result;
if (result.IsSuccessStatusCode)
{
contentElements.AddRange((IEnumerable<ContentElement>)result.Content.ReadAsAsync<List<ContentElement>>().Result);
}
}
return contentElements;
}
The JSON: JSON:
[{"PageId":1,"LanguageCode":"en","ContentId":5,"PageName":"Greeting",
"Content":"Content goes here","DisplayOrder":1,"Image":null,"ContentType":"Text"}]
Your json has "Image" and"ContentType" properties, but your class does not.您的 json 具有“Image”和“ContentType”属性,但您的类没有。 Add these as properties to your class.
将这些作为属性添加到您的类中。 .NET cannot deserialize the json in your class because it does not have the EXACT same properties as the class.
.NET 无法反序列化您的类中的 json,因为它没有与类完全相同的属性。
public class ContentElement : ICloneable
{
public object Clone()
{
return this.MemberwiseClone();
}
public int PageId { get; set; }
public string LanguageCode { get; set; }
public int ContentId { get; set; }
public string PageName { get; set; }
public string Content { get; set; }
public int? DisplayOrder { get; set; }
public byte[] Image { get; set; } //not sure what type this should be
public string ContentType { get; set; }
}
EDIT A workaround fix for this is to use Newtonsoft.Json to deserialize the json instead of using the .NET function.编辑对此的解决方法是使用 Newtonsoft.Json 反序列化 json 而不是使用 .NET 函数。
contentElements.AddRange((IEnumerable<ContentElement>)JsonConvert.Deserialize<IEnumerable<ContentElement>>(result.Content));
public async static Task<List<ContentElement>> GetContent(int siteId)
{
var contentElements = new List<ContentElement>();
using (HttpClient client = new HttpClient())
{
client.BaseAddress = new Uri(GlobalValues.APIUri);
client.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
AddAuthenticationHeader(client);
var result =await client.GetAsync("api/Content/GetContent/" + (object)siteId);
if (result.IsSuccessStatusCode)
{
contentElements.AddRange((IEnumerable<ContentElement>)result.Content.ReadAsStringAsync<List<ContentElement>>());
}
}
return contentElements;
}
make the method async and use await like this使方法异步并像这样使用等待
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.