Web API complex parameter properties are all null

前端 未结 18 1497
长情又很酷
长情又很酷 2020-11-30 04:18

I have a Web API service call that updates a user\'s preferences. Unfortunately when I call this POST method from a jQuery ajax call, the request parameter object\'s proper

相关标签:
18条回答
  • 2020-11-30 04:58

    Today, I've the same problem as yours. When I send POST request from ajax the controller receive empty object with Null and default property values. The method is:

    [HttpPost]
    public async Task<IActionResult> SaveDrawing([FromBody]DrawingModel drawing)
    {
    
    
        try
        {
    
            await Task.Factory.StartNew(() =>
            {
                //Logic
            });
            return Ok();
        }
        catch(Exception e)
        {
            return BadRequest();
        }
    }
    

    My Content-Type was correct and everything else was correct too. After trying many things I found that sending the object like this:

    $.ajax({
        url: '/DrawingBoard/SaveDrawing',
        type: 'POST',
        contentType: 'application/json',
        data: dataToPost
    }).done((response) => { }); 
    

    Won't work, but sending it like this instead worked:

    $.ajax({
        url: '/DrawingBoard/SaveDrawing',
        type: 'POST',
        contentType: 'application/json',
        data: JSON.stringify(dataToPost)
    }).done((response) => { }); 
    

    Yes, the missing JSON.stringify() caused the whole issue, and no need to put = or anything else as prefix or suffix to JSON.stringify. After digging a little bit. I found that the format of the request payload completely different between the two requests
    This is the request payload with JSON.stringify

    And this is the request payload without JSON.stringify

    And don't forget, when things get magical and you use Google Chrome to test your web application. Do Empty cache and hard reload from time to time.

    0 讨论(0)
  • 2020-11-30 04:59

    Using this technique posted by @blorkfish worked great:

    public HttpResponseMessage Post(Object model)
        {
            var jsonString = model.ToString();
            PreferenceRequest result = JsonConvert.DeserializeObject<PreferenceRequest>(jsonString);
        }
    

    I had a parameter object, which had a couple of native types and a couple more objects as properties. The objects had constructors marked internal and the JsonConvert.DeserializedObject call on the jsonString gave the error:

    Unable to find a constructor to use for type ChildObjectB. A class should either have a default constructor, one constructor with arguments or a constructor marked with the JsonConstructor attribute.

    I changed the constructors to public and it is now populating the parameter object when I replace the Object model parameter with the real object. Thanks!

    0 讨论(0)
  • 2020-11-30 04:59

    I ran into the same issue, the solution for me was to make certain the types of my class attributes matched the json atributes, I mean

    Json: "attribute": "true"
    

    Should be treated as string and not as boolean, looks like if you have an issue like this all the attributes underneath the faulty attribute will default to null

    0 讨论(0)
  • 2020-11-30 05:03

    I know, this is a bit late, but I just ran into the same issue. @blorkfish's answer worked for me as well, but led me to a different solution. One of the parts of my complex object lacked a parameter-less constructor. After adding this constructor, both Put and Post requests began working as expected.

    0 讨论(0)
  • 2020-11-30 05:04

    I ran into the same problem today as well. After trying all of these, debugging the API from Azure and debugging the Xamarin Android app, it turns out it was a reference update issue. Remember to make sure that your API has the Newtonsoft.JSON NUGET package updated (if you are using that).

    0 讨论(0)
  • 2020-11-30 05:04

    HttpGet

    public object Get([FromBody]object requestModel)
    {
        var jsonstring = JsonConvert.SerializeObject(requestModel);
        RequestModel model = JsonConvert.DeserializeObject<RequestModel>(jsonstring);
    }
    
    0 讨论(0)
提交回复
热议问题