The input was not valid .Net Core Web API

Don't use FromBody. You're submitting as x-www-form-urlencoded (i.e. standard HTML form post). The FromBody attribute is for JSON/XML.

You cannot handle both standard form submits and JSON/XML request bodies from the same action. If you need to request the action both ways, you'll need two separate endpoints, one with the param decorated with FromBody and one without. There is no other way. The actual functionality of your action can be factored out into a private method that both actions can utilize, to reduce code duplication.


I just worked through a similar situation here; I was able to use the [FromBody] without any issues:

public class MyController : Controller
{
   [HttpPost]
   public async Task<IActionResult> SomeEndpoint([FromBody]Payload inPayload)
   {
   ...
   }
}

public class Payload
{
   public string SomeString { get; set; }
   public int SomeInt { get; set; }
}

The challenge I figured out was the ensure that the requests were being made with the Content-Type header set as "application/json". Using Postman my original request was returned as "The input was not valid." Adding the Content-Type header fixed the issue for me.