how to post arbitrary json object to webapi

前端 未结 4 964
甜味超标
甜味超标 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:34

    We passed json object by jquery, and parse it in dynamic object. it works fine. this is sample code:

    ajaxPost:
    
    ...
    Content-Type: application/json,
    data: {
              "name": "Jack", 
              "age": "12"
          }
    ...
    

    webapi:

    [HttpPost]
    public string DoJson2(dynamic data)
    {
        string name = data.name;
        int age = data.age;
    
        return name;
    }
    

    similary question on stackoverflow: WebAPI Multiple Put/Post parameters

提交回复
热议问题