Post received by FromBody causes serializable error

后端 未结 2 1673
北荒
北荒 2021-01-12 18:29

Here\'s the basic setup, I have an asp.net core webapi controller (in c#) with a post function like so:

[HttpPost]
public ActionResult Post([Fr         


        
2条回答
  •  遥遥无期
    2021-01-12 19:12

    You need a body in which json data will be parsed.

    [FromBody] string Name
    

    can not work with following json

    {
        "Name": "Foo"
    }
    

    It needs a class

    public class MyClass 
    { 
        public string Name;
    }
    

    Then pass it as

    ([FromBody] MyClass obj)
    

    Or if it is single value, use JSON like

    {
        [
           "Foo",
           "Foo1"
        ]
    }
    

    Then pass it as

    ([FromBody] List obj)
    

提交回复
热议问题