0

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

Rob C
  • 818
  • 1
  • 12
  • 26
  • What happens if you inherit from `ApiController` instead of `ControllerBase` – traveler3468 Jun 10 '21 at 06:16
  • https://stackoverflow.com/questions/38667445/is-apicontroller-deprecated-in-net-core `No more need to define [FromBody], [FromRoute], ... attributes explicitly` – traveler3468 Jun 10 '21 at 06:18
  • the working .NET Standard part is irrelevant... Are you sure you didn't make any changes in `Startup.cs` compared to what got generated? in the meantime put `using` on the top of the file... if it doesn't work - post the project on GitHub – Felix Jun 10 '21 at 06:18
  • ApiController is in the System.Web namespace (https://stackoverflow.com/questions/40912473/can-system-web-be-used-with-asp-net-core-with-full-framework) – Rob C Jun 10 '21 at 06:22
  • @AndrewE - Microsoft generates inherit from `ControllerBase`. Why would you recommend changing it?! – Felix Jun 10 '21 at 06:22
  • I tried with/without [FromBody] and neither worked – Rob C Jun 10 '21 at 06:22
  • Indeed, no changes made to Startup.cs or any other parts of the project. Just a vanilla webapi (the one with weather forecast) with a new "values" controller – Rob C Jun 10 '21 at 06:23
  • @Felix was just a suggestion incase something else was modified :) – traveler3468 Jun 10 '21 at 06:26
  • @RobC yes; [FromBody] is not necessary, but doesn't break change anything.Vanilla webapi *does* work... so there *are* some changes. Generate once more - and if it still doesn't work, post on Github – Felix Jun 10 '21 at 06:26
  • Ok will do @Felix . Thanks both – Rob C Jun 10 '21 at 06:35
  • @Felix please see https://github.com/azteconline/CoreWebApi – Rob C Jun 10 '21 at 06:41

1 Answers1

2

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!

Felix
  • 9,248
  • 10
  • 57
  • 89