【发布时间】:2015-04-02 17:18:44
【问题描述】:
正如标题所说,我正在尝试从 Web API 获取响应并将其绑定到下拉列表。无论如何使用他们的documentation 中描述的HttpClient 方法来做到这一点? API 正在返回 JSON 格式的对象列表。
这是我的代码:
using (HttpClient client = new HttpClient(handler))
{
client.BaseAddress = new Uri("MyURI");
HttpResponseMessage response = client.GetAsync("Resource").Result;
if (response.IsSuccessStatusCode)
{
dynamic content = response.Content.ReadAsStringAsync().Result;
//Somehow bind to DropDown
//MyDdl.DataSource = content;
}
}
我正在考虑尝试将其转换为 DataTable 以进行绑定,但这样做似乎需要付出很多努力。
更新
这是返回的内容
[
{"ID":"1","Name":"Bob","Authorizations":[]},
...
]
注意Authorizations 的属性之一本身是一个列表,我假设我必须遍历并省略该属性才能将DropDown 与DataTextField = Name 和DataValueField = ID 绑定。
【问题讨论】:
-
前端是Webforms吗?
-
@Pharylon 是的,WebForms。
-
能否粘贴从语句 response.Content.ReadAsStringAsync().Result 收到的内容;
标签: c# asp.net json asp.net-web-api dotnet-httpclient