问题描述
我在显示从 Get Request 到 MVC Controller 的数据时遇到问题。我想从 Kanbanize API 获取我的数据并将其显示在我的本地应用程序上。我的状态 200 正常,但是当我想将它添加到我的控制器中并查看某些东西时出现问题,我无法理解是什么。
这是我的 Get 请求,状态代码为 200 OK:
List<Board> boards = null;
string response = string.Empty;
var url = "https://<subdomain>.kanbanize.com/api/v2/boards/4";
var httpRequest = (HttpWebRequest)WebRequest.Create(url);
httpRequest.Method = "GET";
httpRequest.Headers.Add("apikey","");
httpRequest.ContentType = "application/json";
ServicePointManager.SecurityProtocol = SecurityProtocolType.Tls12 | SecurityProtocolType.Tls11 | SecurityProtocolType.Tls;
HttpWebResponse httpResponse = (HttpWebResponse)httpRequest.GetResponse();
using (StreamReader streamReader = new StreamReader(httpResponse.GetResponseStream()))
{
response = streamReader.ReadToEnd();
}
httpResponse.Close();
boards = Deserialize<List<Board>>(response);
return View(boards);
}
public static T Deserialize<T>(string jsonData)
{
JsonSerializer json = new JsonSerializer();
return json.Deserialize<T>(new JsonTextReader(new StringReader(jsonData)));
}
从这里我得到了我想要显示的信息,但之后在控制器中一切都变得非常混乱。 谁能告诉我我做错了什么以及正确的方法是什么。 提前致谢!
解决方法
对 https://<subdomain>.kanbanize.com/api/v2/boards/4
的响应返回单个板详细信息而不是列表。
不要声明 Board 的列表,而是声明一个实例,然后再试一次。