Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Asp.net Core 2 API POST Objects are NULL?

I have a .net Core 2 API setup with some test function. (Visual Studio 2017)

Using postman I do a post with the raw data to that method, but the model is just blank? Why?

        // POST api/Product/test
        [HttpPost]
        [Route("test")]
        public object test(MyTestModel model)
        {
            try
            {
                var a = model.SomeTestParam;

                return Ok("Yey");
            }
            catch (Exception ex)
            {
                return BadRequest(new { message = ex.Message });
            }
        }

        public class MyTestModel
        {
            public int SomeTestParam { get; set; }

        }

enter image description here

enter image description here

like image 936
Ruan Avatar asked Aug 24 '17 13:08

Ruan


People also ask

Why to use FromBody in Web api?

Using [FromBody]When a parameter has [FromBody], Web API uses the Content-Type header to select a formatter. In this example, the content type is "application/json" and the request body is a raw JSON string (not a JSON object).

Why your FromBody parameter is always null?

If you need to get multiple values from the request body, define a complex type. But still the value of email is NULL . The JavaScript code is part of generic method we use, so that's why the content-type is set to application/json; charset=utf-8 .

What does FromBody mean in c#?

The [FromBody] attribute which inherits ParameterBindingAttribute class is used to populate a parameter and its properties from the body of an HTTP request. The ASP.NET runtime delegates the responsibility of reading the body to an input formatter.

What is difference between controller and ControllerBase?

Controller derives from ControllerBase and adds support for views, so it's for handling web pages, not web API requests. There's an exception to this rule: if you plan to use the same controller for both views and web APIs, derive it from Controller.


Video Answer


2 Answers

You need to include the [FromBody] attribute on the model:

[FromBody] MyTestModel model

See Andrew Lock's post for more information:

In order to bind the JSON correctly in ASP.NET Core, you must modify your action to include the attribute [FromBody] on the parameter. This tells the framework to use the content-type header of the request to decide which of the configured IInputFormatters to use for model binding.

As noted by @anserk in the comments, this also requires the Content-Type header to be set to application/json.

like image 159
Kirk Larkin Avatar answered Oct 24 '22 04:10

Kirk Larkin


To add more information to the accepted answer:

There are three sources from which parameters are bound automatically without the use of an Attribute:

Form values: These are form values that go in the HTTP request using the POST method. (including jQuery POST requests).

Route values: The set of route values provided by Routing

Query strings: The query string part of the URI.

Note that Body is NOT one of them (though I think it should be).

So if you have values that need to be bound from the body, you MUST use the attribute binding attribute.

This tripped me up yesterday as I assumed that parameters from the Body would be bound automatically.

The second minor point is that only one parameter can be bound to the Body.

There can be at most one parameter per action decorated with [FromBody]. The ASP.NET Core MVC run-time delegates the responsibility of reading the request stream to the formatter. Once the request stream is read for a parameter, it's generally not possible to read the request stream again for binding other [FromBody] parameters.

Thus if there is more than one parameter you need, you need to create a Model class to bind them:

public class InputModel{
   public string FirstName{get;set;}
   public string LastName{get;set;}
}

[HttpPost]
public IActionResult test([FromBody]InputModel model)...

The Docs

like image 37
Greg Gum Avatar answered Oct 24 '22 03:10

Greg Gum