微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

MVC HTTP POST 请求

如何解决MVC HTTP POST 请求

我在显示从 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 的列表,而是声明一个实例,然后再试一次。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。