ASP.NET Core API POST parameter is always null

前端 未结 9 542
自闭症患者
自闭症患者 2020-12-14 00:27

I have read the following:

  • Asp.net Core Post parameter is always null
  • asp.net webapi 2 post parameter is always null
  • web-api POST body object
9条回答
  •  醉酒成梦
    2020-12-14 00:57

    My particular issue was that the model binding was silently failing for a JSON model. (It was always null).

    As I had the exact JSON being posted, I was able to debug it locally by running the web-service locally, and posting to my controller via cURL (can use POSTMAN).

    Using the below code, I was able to see the exact exception occurring during serialization.

        [HttpPost]
        public IActionResult MyAction([FromBody] dynamic request)
        {            
            if (request != null)
            {
                try
                {
                    var objAttempt =
                        JsonConvert.DeserializeObject(
                            JsonConvert.SerializeObject(request));
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception);
                    throw;
                }
            }
    

提交回复
热议问题