web-api POST body object always null

前端 未结 26 2797
余生分开走
余生分开走 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.

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

    I spend several hours with this issue... :( Getters and setters are REQUIRED in POST parameters object declaration. I do not recommend using simple data objects (string,int, ...) as they require special request format.

    [HttpPost]
    public HttpResponseMessage PostProcedure(EdiconLogFilter filter){
    ...
    }
    

    Does not work when:

    public class EdiconLogFilter
    {
        public string fClientName;
        public string fUserName;
        public string fMinutes;
        public string fLogDate;
    }
    

    Works fine when:

    public class EdiconLogFilter
    {
        public string fClientName { get; set; }
        public string fUserName { get; set; }
        public string fMinutes { get; set; }
        public string fLogDate { get; set; }
    }
    
    0 讨论(0)
  • 2020-11-27 15:21

    It can be helpful to add TRACING to the json serializer so you can see what's up when things go wrong.

    Define an ITraceWriter implementation to show their debug output like:

    class TraceWriter : Newtonsoft.Json.Serialization.ITraceWriter
    {
        public TraceLevel LevelFilter {
            get {
                return TraceLevel.Error;
            }
        }
    
        public void Trace(TraceLevel level, string message, Exception ex)
        {
            Console.WriteLine("JSON {0} {1}: {2}", level, message, ex);
        }
    }
    

    Then in your WebApiConfig do:

        config.Formatters.JsonFormatter.SerializerSettings.TraceWriter = new TraceWriter();
    

    (maybe wrap it in an #if DEBUG)

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

    I had the same problem.

    In my case, the problem was in public int? CreditLimitBasedOn { get; set; } property I had.

    my JSON had the value "CreditLimitBasedOn":true when It should contain an integer. This property prevented the whole object being deserialized on my api method.

    0 讨论(0)
  • 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.

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

    This is a little old one and my answer will go down to the last place but even so I would like to share my experience.

    Tried every suggestion but still having the same "null" value in a PUT [FromBody].

    Finally found it was all about Date format while JSON serializing the EndDate property of my Angular Object.

    No error was thrown, just received an empty FromBody object....

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