web-api POST body object always null

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

    This is another issue related to invalid property values in an Angular Typescript request.

    This is was related to the conversion between a Typescript number to an int(Int32) in C#. I was using Ticks (UTC milliseconds) which is larger than the signed, Int32 range (int in C#). Changed the C# model from int to long and everything worked fine.

    0 讨论(0)
  • 2020-11-27 15:34

    I used HttpRequestMessage and my problem got solved after doing so much research

    [HttpPost]
    public HttpResponseMessage PostProcedure(HttpRequestMessage req){
    ...
    }
    
    0 讨论(0)
  • 2020-11-27 15:36

    I was looking for a solution to my problem for some minutes now, so I'll share my solution.

    When you have a custom constructor within your model, your model also needs to have an empty/default constructor. Otherwise the model can't be created, obviously. Be careful while refactoring.

    0 讨论(0)
  • 2020-11-27 15:36

    I've hit this problem so many times, but actually, it's quite straightforward to track down the cause.

    Here's today's example. I was calling my POST service with an AccountRequest object, but when I put a breakpoint at the start of this function, the parameter value was always null. But why ?!

    [ProducesResponseType(typeof(DocumentInfo[]), 201)]
    [HttpPost]
    public async Task<IActionResult> Post([FromBody] AccountRequest accountRequest)
    {
        //  At this point...  accountRequest is null... but why ?!
    
        //  ... other code ... 
    }
    

    To identify the problem, change the parameter type to string, add a line to get JSON.Net to deserialize the object into the type you were expecting, and put a breakpoint on this line:

    [ProducesResponseType(typeof(DocumentInfo[]), 201)]
    [HttpPost]
    public async Task<IActionResult> Post([FromBody] string ar)
    {
        //  Put a breakpoint on the following line... what is the value of "ar" ?
        AccountRequest accountRequest = JsonConvert.DeserializeObject<AccountRequest>(ar);
    
        //  ... other code ...
    }
    

    Now, when you try this, if the parameter is still blank or null, then you simply aren't calling the service properly.

    However, if the string does contain a value, then the DeserializeObject should point you towards the cause of the problem, and should also fail to convert your string into your desired format. But with the raw (string) data which it's trying to deserialize, you should now be able to see what's wrong with your parameter value.

    (In my case, we were calling the service with an AccountRequest object which had been accidentally serialized twice !)

    0 讨论(0)
  • 2020-11-27 15:36

    If this is because Web API 2 ran into a deserialization problem due to mismatched data types, it's possible to find out where it failed by inspecting the content stream. It will read up until it hits an error, so if you read the content as a string, you should have the back half of the data you posted:

    string json = await Request.Content.ReadAsStringAsync();
    

    Fix that parameter, and it should make it further next time (or succeed if you're lucky!)...

    0 讨论(0)
  • 2020-11-27 15:37

    None of the above was my solution: in my case the issue is that [ApiController] was not added to the controller so it is giving Null value

    [Produces("application/json")]
    [Route("api/[controller]")]
    [ApiController] // This was my problem, make sure that it is there!
    public class OrderController : Controller
    

    ...

    0 讨论(0)
提交回复
热议问题