WebAPI Post方法接收的FromBody一直为null

 // POST api/getjson
        public string PostTest([FromBody]string value)
        {
            return "Got it!";
        }

初学WebAPI,一开始是这样写的,试过用Fiddler发送,用微信小程序的API发送,修改各种content-type都不行,接收到的FromBody一直为null

wx.request({
  url: 'http://localhost:5380/api/getJson/PostTest', //仅为示例,并非真实的接口地址
  data: {
     x: '123' ,
     y: '456'
  },
    method: 'POST',
  header: {
     // 'content-type': 'application/json'
     'content-type': 'application/x-www-form-urlencoded'
  },
  success: function(res) {
    console.log(res.data)
  }
})

最后发现是接收类型不对,改为object就好了,如下:

// POST api/getjson
        public string PostTest([FromBody]object value)
        {
            return "Got it!";
        }

json对于c#来说不就是字符串吗,为什么接收不了,懂的,求指教

posted @ 2016-11-20 21:53  陈夏松  阅读(30555)  评论(7编辑  收藏  举报