1

I'm trying to build an api using asp.net core 2. When posting from Postman the values received are null.

controller

// POST api/customers
[HttpPost]
public Customer Post(Customer customer)
{
   var c = customer;
   c.AddedDate = DateTime.UtcNow;
   context.AddAsync(c);
   context.SaveChangesAsync();
   return c;
 }

model

namespace AspDotNetCore.Models
{
    public class CRUDContext : DbContext
    {
        public CRUDContext(DbContextOptions<CRUDContext> options) : base(options)
        {
        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            base.OnModelCreating(modelBuilder);
            new CustomerMap(modelBuilder.Entity<Customer>());
        }
    }

    public class BaseEntity
    {
        public Int64 Id { get; set; }
        public DateTime AddedDate { get; set; }
        public DateTime ModifiedDate { get; set; }
        public string IPAddress { get; set; }
    }

    public class Customer : BaseEntity
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string Email { get; set; }
        public string MobileNo { get; set; }
    }

    public class CustomerMap
    {
        public CustomerMap(EntityTypeBuilder<Customer> entityBuilder)
        {
            entityBuilder.HasKey(t => t.Id);
            entityBuilder.Property(t => t.FirstName).IsRequired();
            entityBuilder.Property(t => t.LastName).IsRequired();
            entityBuilder.Property(t => t.Email).IsRequired();
            entityBuilder.Property(t => t.MobileNo).IsRequired();
        }
    }
}

postman request

{
    "Email": "bob@bob.com",
    "FirstName": "bob",
    "LastName": "barker",
    "MobileNo": "00000000000"
}
Antarr Byrd
  • 24,863
  • 33
  • 100
  • 188
  • 2
    Possible duplicate of https://stackoverflow.com/questions/45862459/asp-net-core-2-api-post-objects-are-null/45862556#45862556. Please upvote the answer if you find it to be useful ;) – Kirk Larkin Sep 01 '17 at 19:04
  • I think that your `Customer` class will need a default constructor (even if it is empty) to support serialization and deserialization. Just add `public Customer() { }` inside the class and try again. – Dave Smash Sep 01 '17 at 19:06
  • 3
    try `public Customer Post([FromBody] Customer customer)` – gh9 Sep 01 '17 at 19:06
  • @ElementalPete, in C# the default constructor is already given as it is provided from the base class `object`. – Quality Catalyst Sep 01 '17 at 19:07
  • @QualityCatalyst - I thought I had run into that issue previously, but maybe it's only an issue if you include a non-default constructor and you don't also include a default one... I'll defer to you on this one. :) – Dave Smash Sep 01 '17 at 19:13
  • try removing 'Post' once you have added [HttpPost] – Afnan Makhdoom Sep 01 '17 at 19:19
  • Possible duplicate of [Asp.net Core 2 API POST Objects are NULL?](https://stackoverflow.com/questions/45862459/asp-net-core-2-api-post-objects-are-null) – The Bearded Llama Sep 05 '17 at 12:19

1 Answers1

3

As you're passing data in the body, [FromBody] could help here:

[HttpPost]
public Customer Post([FromBody] Customer customer)

You could bind model from various sources, more details could be found here https://tahirnaushad.com/2017/08/22/asp-net-core-2-0-mvc-model-binding/