WEB API POST returns NULL

Asked

Viewed 141 times

0

I am starting my studies with REST API and following an Internet tutorial I developed this API :

Class :

namespace SimpleRESTServer.Models

{ public class Person { public long ID { get; set; } public String Lastname { get; set; } public String Fisrtname { get; set; } public Double Payrate { get; set; } public Datetime Startdate { get; set; } public Datetime Enddate { get; set; } } }

Controller :

namespace SimpleRESTServer.Controllers

{ public class Personcontroller : Apicontroller { // GET: api/Person public Ienumerable Get() { Return new string[] { "Person1", "Person2", "É nois Mano" }; }

    // GET: api/Person/5
    public Person Get(int id)
    {
        Person person = new Person();
        person.ID = id;
        person.LastName = "Da Silva";
        person.FisrtName = "Eduardo";
        person.PayRate = 45.54;
        person.StartDate = DateTime.Parse("5/5/2019");
        person.EndDate = DateTime.Parse("5/10/2019");

        return person;
    }

    // POST: api/Person
    public void Post([FromBody]Person value)
    {
        //PersonPersistence pp = new PersonPersistence();
        //long id;
        //id = pp.savePerson(value);
    }

    // PUT: api/Person/5
    public void Put(int id, [FromBody]string value)
    {
    }

    // DELETE: api/Person/5
    public void Delete(int id)
    {
    }
}

}

And I’m using POSTMAN to test it and fill out POSTMAN according to the screen below.

inserir a descrição da imagem aqui

It turns out that the information is not coming in the Controller when I debug the content it is always NULL

inserir a descrição da imagem aqui

Does anyone have any idea what might be wrong..???

Thank you

1 answer

2


You are sending data through the URL, in which case you would need to use Annotation [Fromuri] instead of [Frombody].

I believe you are doing the wrong tests by Postman, I recommend using RAW and writing your json in hand.

  • 1

    Thanks Carlos. I switched to [Fromuri] as you suggested and worked.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.