Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.Net Core Model Binding JSON Post To Web API

Just started a new project using .NET Core. Added my Web API controller and related method. Using Postman I created a JSON object and posted it to my controller method. Bear in mind the JSON object matches the Object param in the controller method exactly.

In debug mode I can see the object, it is not null, the properties are there, HOWEVER the prop values are defaulted to their representatives types, 0 for int, etc.

I've never seen this behavior before… so I took exactly the same code and object and replicated in a MVC project with a Web API 2 controller and it works perfectly.

What am I missing, can I not POST JSON and model bind in .NET Core?

Reading this article it seems I cannot unless I send as form POST or as querystring vars which by the way works fine.

https://lbadri.wordpress.com/2014/11/23/web-api-model-binding-in-asp-net-mvc-6-asp-net-5/

JSON:

{
   "id": "4",
   "userId": "3"
   "dateOfTest": "7/13/2017"
}

Method:

[HttpPost]
[Route("test1")]
[AllowAnonymous]
public IActionResult Test(Class1 data)
{
    return Ok();
}
like image 757
Mike Anderson Avatar asked Jul 13 '17 16:07

Mike Anderson


People also ask

How do I pass model to Web API POST method?

Answers. You can serialize the model into a json string and transfer it, and then deserialize the json string in the get method of WebAPI to get the model.

How do I post json to a REST API endpoint?

To post JSON to a REST API endpoint, you must send an HTTP POST request to the REST API server and provide JSON data in the body of the POST message. You also need to specify the data type in the body of the POST message using the Content-Type: application/json request header.


2 Answers

NOTE: If you are using aspnet core 3.0, the solution can be found here. For other versions, keep reading.

You need to mark your parameter as coming from the body with the FromBody attribute like this:

[HttpPost]
[Route("test1")]
[AllowAnonymous]
public IActionResult Test([FromBody] Class1 data)
{
    return Ok();
}

You need to make sure you're using application/json as your content type from Postman:

Postman application/json

Resulting in:

json POST action

Make sure your property setters are public as well:

public class Person
{
    public String Name;
    public Int32 Age;
}
like image 125
Matt Avatar answered Sep 19 '22 04:09

Matt


I was struggling with this too, and thought I would share one item that was necessary for me and not in the accepted answer, I needed to add {get; set;} to each attribute in my class as so:

    public class LogString{
        public string val {get; set;}
        public string data {get; set;}
    }

The rest was the same:

        [HttpPost]
        public void Post([FromBody] LogString message)
        {      
            Console.WriteLine(message.val);
        }

After adding that it started working.

like image 45
edencorbin Avatar answered Sep 23 '22 04:09

edencorbin