web-api POST body object always null

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

提交回复
热议问题