Search code examples
c#.net-coreasp.net-web-api2asp.net-core-webapi.net-standard

WebApi NET Core not receiving request json, whereas it does in NET standard


I created a NET Core starter WebApi project and added a very simple method/object. Testing the endpoint with Fiddler the request body doesnt bind to the post param. I had spent 2 hours of searching for a solution to no avail. For brevity I included my object in the controller.

namespace CoreWebApi.Controllers
{
    using Microsoft.AspNetCore.Mvc;

    [Route("[controller]")]
    [ApiController]
    public class ValuesController : ControllerBase
    {
        [HttpPost]
        public Message Post([FromBody]Message message)
        {
            return message;
        }
    }

    public class Message
    {
        public string Application;

        public string StatusOverride;
    }
}

Fiddler

NET Core

After a lot of head scratching, I created a NET Standard web api starter project, copy/paste of my method and object and worked first time! Wondering if someone can enlighten me as to what I would need to do on my NET core webapi to get it working.

using System.Web.Http;

namespace WebApplication9.Controllers
{
    public class ValuesController : ApiController
    {
        public Message Post([FromBody]Message message)
        {
            return message;
        }
    }

    public class Message
    {
        public string Application;

        public string StatusOverride;
    }
}

NET Standard


Solution

  • Even though class members are public, they still need to have get/set accessors:

    public class Message
    {
        public string Application { get; set; }
        public string StatusOverride { get; set; }
    }
    

    ...and it works!