how to post arbitrary json object to webapi

前端 未结 4 981
甜味超标
甜味超标 2020-12-14 01:20

How do I / is it possible to pass in a json object to a webapi controller (POST) and not have a class to map it to, but rather handle it as arbitrary content?

4条回答
  •  天命终不由人
    2020-12-14 01:28

    You can have your post method that takes in a HttpRequestMessage to by pass the model binding logic and you can read the content of the request directly:

        public HttpResponseMessage Post(HttpRequestMessage req)
        {
            var data = req.Content.ReadAsStringAsync().Result; // using .Result here for simplicity...
            ...
    
        }
    

    By the way, the reason why the action that takes in JObject doesn't work is because of 'ObjectId("...")' that is used as the value of "_id" in your data...

提交回复
热议问题