Object getting null value FromForm in ASP.NET Core Web API

Viewed 71

API controller and the request send from react app.

When using FromForm only then getting null value

    [Route("PotService")]
    [HttpPost]
    [Consumes("application/json")]
    public IActionResult Post([FromForm] ServiceInfo serviceInfo)
    {
        //using (MemoryStream stream = new MemoryStream())
        //{
        //    service.Images.CopyToAsync(stream);
        //}
        bool result = _serManager.AddService(serviceInfo);
        return Ok(result);
    }
1 Answers

The Consumes attribute specifies data types that an action accepts. If you are using application/json, it will receive the parameters transferred via the request body. And from your frontend code, it also seems that you are transfer the data from body, so, try to use the [FromBody] attribute and use the following code.

[Route("PotService")]
[HttpPost]
[Consumes("application/json")]
public IActionResult Post([FromBody] ServiceInfo serviceInfo)
{
    //using (MemoryStream stream = new MemoryStream())
    //{
    //    service.Images.CopyToAsync(stream);
    //}
    bool result = _serManager.AddService(serviceInfo);
    return Ok(result);
}

enter image description here

If you want to send the parameters via the Form, try to use application/x-www-form-urlencoded or multipart/form-data data type.

Refer to the following screenshot:

enter image description here

Related