使用HttpClient消费odata web api [英] Consume odata web api with a HttpClient
问题描述
嘿伙计们!
我开发了一个支持odata的ASP.NET Web Api(2.2)。 API运行正常,我用fiddler来发布,发布东西,api处理好一切。
现在我正在尝试编写一个消耗API的C#库。现在我曾经有一个没有odata支持的API,下面的代码工作正常:
Hey guys!
I developed a ASP.NET Web Api (2.2) with odata support. The API works fine, I use fiddler to get, post stuff and the api handles everything fine.
Now I'm trying to write a C# library that consumes the API. Now I used to have a API without odata support and the following code works just fine :
var url = "Support/Tickets";
HttpResponseMessage response = client.GetAsync(url).Result;
if (response.IsSuccessStatusCode)
{
var list = response.Content.ReadAsAsync<List<Tickets>>).Result;
}
(此处客户端是System.Net.Http.HttpClient对象)
但是,由于我实现了odata解决方案,因此从API返回的JSON发生了变化:
(client here is a System.Net.Http.HttpClient object)
However, since I implemented the odata solution, the JSON returned from the API is changed :
{
"odata.metadata":"http://url.to/Support/$metadata#Tickets","value":[
{
}
]
}
现在粗线是新的并且导致线
var list = response.Content.ReadAsAsync< List< Tickets>>)。
失败。我明白这条线路失败的事实,但我无法弄清楚正确处理新JSON响应的方法。我需要使用某种odata解串器吗?
谢谢!
Eduard
Now the bold line is new and causes the line
var list = response.Content.ReadAsAsync<List<Tickets>>).Result;
to fail. The fact that the line fails I understand, but I can't figure out what way to handle the new JSON response correctly. Do I need to use some kind of odata deserializer or something?
Thanks!
Eduard
推荐答案
元数据#票证, value:[
{
}
]
}
metadata#Tickets","value":[ { } ] }
现在粗线是新的并导致线
var list = response.Content.ReadAsAsync< List< Tickets>>)。结果;
失败。我理解这条线路失败的事实,但我无法弄清楚正确处理新JSON响应的方法。我是否需要使用某种odata解串器或什么?
谢谢!
Eduard
Now the bold line is new and causes the line
var list = response.Content.ReadAsAsync<List<Tickets>>).Result;
to fail. The fact that the line fails I understand, but I can't figure out what way to handle the new JSON response correctly. Do I need to use some kind of odata deserializer or something?
Thanks!
Eduard
这篇关于使用HttpClient消费odata web api的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!