0
我在控制器中使用帶有以下數據的Web Api。從C#中的Web Api獲取數據
public IEnumerable<string> Get()
{
return new string[] { "Item1", "Item2s", "Item3", "Item4", "Item5" };
}
我想從應用程序中的Web Api中獲取此數據。我用這個代碼從另一個控制器獲取數據:
public IEnumerable<Items> GetItems()
{
return repository.GetItems();
}
上面顯示控制器代碼,可推動在網絡API指定的項目列表。 如何更改以下代碼以從字符串[]中獲取數據?
HttpClient client = new HttpClient();
HttpResponseMessage response = await client.GetAsync("http://localhost:1234/api/items");
var items = new List<Items>();
if (response.IsSuccessStatusCode)
{
var content = await response.Content.ReadAsStringAsync();
// Parse 1 Product from the content
var ItemsSet= JsonConvert.DeserializeObject<dynamic>(content);
// Data from Api
var ItemData = new Items
(
(string)ItemsSet[0],
(string)ItemsSet[1],
(string)ItemsSet[2],
(string)ItemsSet[3],
(string)ItemsSet[4]
);
items.Add(ItemData);
}
這沒有工作,還我做編輯的代碼,我犯了一個錯誤。它應該是'items.Add(ItemData);' – Tester
你用這段代碼得到了什麼錯誤信息? –
幾個因爲我必須將數組值添加到'Items'對象。因此,'新列表();' –
Tester