web-api POST body object always null

前端 未结 26 3048
余生分开走
余生分开走 2020-11-27 15:03

I\'m still learning web API, so pardon me if my question sounds stupid.

I have this in my StudentController:

public HttpResponseMessage          


        
26条回答
  •  我在风中等你
    2020-11-27 15:22

    FromBody is a strange attribute in that the input POST values need to be in a specific format for the parameter to be non-null, when it is not a primitive type. (student here)

    1. Try your request with {"name":"John Doe", "age":18, "country":"United States of America"} as the json.
    2. Remove the [FromBody] attribute and try the solution. It should work for non-primitive types. (student)
    3. With the [FromBody] attribute, the other option is to send the values in =Value format, rather than key=value format. This would mean your key value of student should be an empty string...

    There are also other options to write a custom model binder for the student class and attribute the parameter with your custom binder.

提交回复
热议问题