Ruan
Ruan

Reputation: 4293

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

Upvotes: 17

Views: 14877

Answers (5)

reckface
reckface

Reputation: 5878

Alternatively, add the [ApiController] attribute to your controller.

This has annoyingly affected me so many times (months apart) that I want this answer visible.

Upvotes: 1

JF Gagnon
JF Gagnon

Reputation: 221

In my case I had { get; set; } missing in my .cs model which results in an object with all members null on POST.

Upvotes: 0

Mahdi Jalali
Mahdi Jalali

Reputation: 333

I deal with this issue for some hours. This problem stems from several reasons. Let's consider the request is Reactjs (javascript) and backend (API) is Asp .Net Core.

in the request, you must set in header Content-Type:

Axios({
            method: 'post',
            headers: { 'Content-Type': 'application/json'},
            url: 'https://localhost:44346/Order/Order/GiveOrder',
            data: order,
          }).then(function (response) {
            console.log(response);
          });

and in the backend (Asp .net core API) u must have some setting:

1. in Startup --> ConfigureServices:

#region Allow-Orgin
            services.AddCors(c =>
            {
                c.AddPolicy("AllowOrigin", options => options.AllowAnyOrigin());
            });
            #endregion

2. in Startup --> Configure before app.UseMvc() :

app.UseCors(builder => builder
                .AllowAnyOrigin()
                .AllowAnyMethod()
                .AllowAnyHeader()
                .AllowCredentials());

3. in the controller before action:

[EnableCors("AllowOrigin")]

Upvotes: 0

Greg Gum
Greg Gum

Reputation: 38109

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

Upvotes: 4

Kirk Larkin
Kirk Larkin

Reputation: 93273

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.

Upvotes: 30

Related Questions