web-api POST body object always null

前端 未结 26 3032
余生分开走
余生分开走 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:19

    I just ran into this and was frustrating. My setup: The header was set to Content-Type: application/JSON and was passing the info from the body with JSON format, and was reading [FromBody] on the controller.

    Everything was set up fine and I expect it to work, but the problem was with the JSON sent over. Since it was a complex structure, one of my classes which was defined 'Abstract' was not getting initialized and hence the values weren't assigned to the model properly. I removed the abstract keyword and it just worked..!!!

    One tip, the way I could figure this out was to send data in parts to my controller and check when it becomes null... since it was a complex model I was appending one model at a time to my request params. Hope it helps someone who runs into this stupid issue.

提交回复
热议问题