.Net Core Model Binding JSON Post To Web API

Viewed 45219

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();
}
6 Answers

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.

I have an ASP.Net Core 3.1 application and for me, none of these solutions worked. I lost a few hairs (and hours of work) until I found the documentation https://docs.microsoft.com/en-us/aspnet/core/web-api/jsonpatch?view=aspnetcore-3.1

In short :

  1. Install the Microsoft.AspNetCore.Mvc.NewtonsoftJson NuGet package.
  2. Update the project's Startup.ConfigureServices method to call AddNewtonsoftJson. For example:

    public void ConfigureServices(IServiceCollection services) { services.AddRazorPages() .AddNewtonsoftJson();//this is important }

That's it ! I don't need the [FromBody] attribute.

If it doesn't work, verify that your model's properties have setters (for example public string Message { get; set; }). Don't forget the setter.

Consider Properties instead of Fields on your binding objects

On .NET 5, I had to add {get; set;} to my public fields to turn them into properties to get them to populate.

Either the model binder in .NET 5 can't work with fields or you have to do something special to make it populate them. I don't know which. It took a few minutes to turn all my fields into properties, so I wasn't going to spend time trying to figure out how to configure it to work with fields.

Note: If you have this problem with request objects, you will probably also have the same issue with response objects.

I have been struggling with this issue for several hours and none of existing solutions worked for me, but turned out that I had no public parameterless constructor for class of incoming instance. In this case, Class1 had only protected constructors and parameter was always null no matter what. Hope it helps somebody.

If anyone else is coming to this question and like me you are used to working with .Net Framework MVC you may be creating the JSON object similar to this -

 this.http.post<Stock>("/api/Stock2", { Stock: this.SelectedStock }, this.httpOptions)

and then expecting it to bound to 'Stock'

[HttpPost("Stock2")]
    public JsonResult Stock2(ConsolidatedStock Stock) {
        return this.Json(1);
    }

This will no longer work in .net core, you need to change how you build the JSON -

 this.http.post<Stock>("/api/Stock2", this.SelectedStock, this.httpOptions)

If you want to post multiple objects, you would need to create a new model to hold those objects, for example

public class StockViewModel {
    public ConsolidatedStock Stock { get; set; }
}
Related