Web API - 调试显示混乱的信息 [英] Web API - debugging shows confusing information

查看:174
本文介绍了Web API - 调试显示混乱的信息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经获得了



然而,在使用建议从答案和评论更改代码之后:

 命名空间NA.Controllers 
{
public class NotesController:ApiController
{

[Route(AddNote)]
[HttpPost]
public HttpResponseMessage PostNote()
{
HttpResponseMessage response = new HttpResponseMessage();
StreamReader stream = new StreamReader(HttpContext.Current.Request.InputStream);
string jsonData = stream.ReadToEnd();
System.IO.File.WriteAllText(@C:\temp\BLZ_content.txt,jsonData);
返回响应;
}

}
}

我得到一个成功,正确的响应和json被保存到一个文件。所以API一般工作正常。这反过来意味着我的反序列化json代码(或者仅仅从体内捕获)不是。

解决方案

不会从刚刚创建的响应对象中读取传入的数据。你必须从请求中读取它:

  string jsonData = new StreamReader(Request.InputStream).ReadToEnd(); 

然后,您可以将其记录到文件中,看看是否获得了您期望获得的内容。如果你这样做,你可以反序列化手动jsonData到列表,如下所示:

 列表<注意> response = JsonConvert.DeserializeObject< List< Note>>(jsonData); 


I've already got help here and here with my Web API, but now I think I've arrived to the edge of a Grand Canyon... Fortunately my execution by my boss was postponed, but sentence was not yet commuted. So any suggestions appreciated wholeheartedly, especially since my newbie status in the stuff hasn't yet changed.

So, the code is as shown in second linked question (can post it here, but I think it would be redundant). I've corrected errors with SQL link, so it now doesn't crash when trying to call procedure, set up debug enviro and started testing.

I'm sending a POST request (with JSON in payload) using ARC extension in Chrome while debugging and I have an error message:

However, after changing code using suggestions from answers and comments to this:

namespace NA.Controllers
{
    public class NotesController : ApiController
    {

        [Route("AddNote")]
        [HttpPost]
        public HttpResponseMessage PostNote()
        {
            HttpResponseMessage response = new HttpResponseMessage();
            StreamReader stream = new StreamReader(HttpContext.Current.Request.InputStream);
            string jsonData = stream.ReadToEnd();
            System.IO.File.WriteAllText(@"C:\temp\BLZ_content.txt", jsonData);
            return response;
        }

    }
}

I get a success, correct response and json is being saved to a file. So API in general works fine. This, in turn, means that my code for deserialize json (or just capturing it from body) is not.

解决方案

You can not read incoming data from the response object you just created. You have to read it from the Request like this:

string jsonData = new StreamReader(Request.InputStream).ReadToEnd();

Then you can log it to the file to see if you are getting what you are expecting to get. If you do, you can then deserialize manually jsonData to List like this:

List<Note> response = JsonConvert.DeserializeObject<List<Note>>(jsonData);

这篇关于Web API - 调试显示混乱的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆