WebApi Json Id无法检索所有其他字段显示的位置

时间:2017-10-19 15:17:06

标签: jquery json rest api model-view-controller

我是MVC的初学者,最近开始使用web apis开发测试站点,我有我的api控制器名称CustomerController,它返回简单的IEnumerable客户列表。

当我用POSTMAN测试我的api时它的显示记录但Id显示0(id = 0)下面的图像是供参考的         [Ids显示0] [1]           [1]:https://i.stack.imgur.com/4MqUb.png

    below is my api/controller


        public class CustomersController : ApiController{
            private ApplicationDbContext _context;

            public CustomersController(){
                _context = new ApplicationDbContext();
            }

            //GET api/Customer
            public IEnumerable<CustomerDto> GetCustomers(){
               return _context.Customer.ToList().Select(Mapper.Map<Customer,CustomerDto>);
            }

>CUstomer MODEL

        public class Customer
        {
                public int id{ get; set; }
                [Required(ErrorMessage = "Please enter your name")]
                [StringLength(255)]
                public string Name{ get; set; }
                public bool IsSubscribeToNewsLetter{ get; set; }
                public MembershipType MembershipType{ get; set; }
                [Display(Name = "Membership Type")]
                public byte MembershipTypeId{ get; set; }
                [Display(Name = "Date of Birth")]
                [Min18YearsIfMember]
                public DateTime? BirthDate{ get; set; }
        }
    }



>CUSTOMERDTO MODEL 


     public class CustomerDto
        {
            public int id { get; set; }
            [Required(ErrorMessage = "Please enter your name")]
            [StringLength(255)]
            public string Name { get; set; }
            public bool IsSubscribeToNewsLetter { get; set; }
            public byte MembershipTypeId { get; set; }
            //[Min18YearsIfMember]
            public DateTime? BirthDate { get; set; }
        }
       <i/>

1 个答案:

答案 0 :(得分:0)

最后我找到了上述问题的原因:问题出现在我的映射配置文件中,我已经像这样映射了

 Mapper.CreateMap<CustomerDto, Customer>().ForMember(c=>c.id, opt=> opt.Ignore());
  Mapper.CreateMap<Customer, CustomerDto>().ForMember(c => c.id, opt => opt.Ignore());

在上面的映射中我忽略了id字段,因为当我将我的映射编辑为简单的时,我的id显示为0

 Mapper.CreateMap<CustomerDto, Customer>();
  Mapper.CreateMap<Customer, CustomerDto>();

通过以上操作更改我的api在我的网页上显示ID。 P.S因为我是初学者,我仍然需要找到上述变化的影响/