Web API,C#,Json对象,视图

时间:2017-08-18 13:38:59

标签: json asp.net-web-api

我在这个ASP.NET Web API项目中将数据显示为Json对象时遇到了一些问题。这是我的第一次尝试,我没有意见,我将使用Postman进行测试。你能给我一些指示如何将模型显示为Json对象吗?

//UserController

           public class UsersController : ApiController
                {
                    private LearnToLearnContext db = new LearnToLearnContext();
                    private BaseRepository<Users> _repository = null;

                    public UsersController()
                    {
                        this._repository = new BaseRepository<Users>();
                    }

                    // GET: api/Users
                    [ResponseType(typeof(Users))]
                    public IHttpActionResult GetUsers()
                    {
                        var user = _repository.GetAll();
                        var bindingModel = Mapper.Map<UsersBindingModels>(user);

                        return Ok(bindingModel);
                    }
            }


//UserModel

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; }
    } 

//UserBindingModel
 public class UsersBindingModels
    {
        [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; }
    }

0 个答案:

没有答案