lundi 21 août 2017

POST method, Web API, AutoMapper, BindingModels

Hello guys! I am dealing with my first Web API I am using BindingModels to represent the data. I have struggles with the POST (and PATCH) methods. Because as I know, you have this attribute [FromBody] and then some parameter value, which you should use to retrieve the data (JSON object) from the body. But I have Models and Binding models (which in my case are "views") and I want to map the Binding model to the Model I am using Postman for testing the project.

http://ift.tt/2uZ4rU3

POST api/Users

 public void PostUser([FromBody]string value)
                {
                    Users user = new Users();
                    UsersBindingModels userBindingModel = new UsersBindingModels();

                    var newUser = Mapper.Map(userBindingModel, user);
                    _repository.Insert(newUser);
                    _repository.Save(); 
                }

My model

public class Users 
   {
            [Key]
            public int Id { get; set; }
            [Required]
            public string Name { get; set; }
            [Unique]
            [Required]
            public string Email { get; set; }
            [Required]
            public string Password { get; set; }
            public bool IsTeacher { get; set; }

            public virtual List<Courses> Courses { get; set; }
   } 

My Binding Model

public class UsersBindingModels
        {
            public int id { get; set; }
            public string name { get; set; }
            public string email { get; set; }
            public string password { get; set; }
            public bool isTeacher { get; set; } 
        }




Aucun commentaire:

Enregistrer un commentaire