Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/290.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/meteor/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 为什么我在web api测试中遇到500个内部服务器错误?_C#_Asp.net_Asp.net Web Api_Internal Server Error - Fatal编程技术网

C# 为什么我在web api测试中遇到500个内部服务器错误?

C# 为什么我在web api测试中遇到500个内部服务器错误?,c#,asp.net,asp.net-web-api,internal-server-error,C#,Asp.net,Asp.net Web Api,Internal Server Error,我写了一个post api来创建用户 [HttpPost] public User CreateUser(User user) { //User jsonToObject = JsonConvert.DeserializeObject<User>(user.ToString()); if (ModelState.IsValid) { db.users.Add(user);

我写了一个post api来创建用户

[HttpPost]
    public User CreateUser(User user)
    {
        //User jsonToObject = JsonConvert.DeserializeObject<User>(user.ToString());
        if (ModelState.IsValid)
        {
            db.users.Add(user);
            db.SaveChanges();
        }
        else
        {
            throw new Exception("The posted Model is not valid");
        }

      return user;

    }
我得到500个内部服务器错误。下面是我的模型,以提供更多信息

 public class User
    {
        public int Id { get; set; }
        [Required]
        public string FirstName { get; set; }
        [Required]
        public string LastName { get; set; }

        [Required]
        public string Email { get; set; }

        [Required]
        public string Password { get; set; }


        [NotMapped]
        [Compare("Password")]
        public string ConfirmPassword { get; set; }
        public int UserType { get; set; }
        public DateTime Date_Created { get; set; }
    }

请帮助。我不熟悉编码,asp.net未设置密码确认密码?最简单的方法是调试您的应用程序并找出ModelState是什么。@Jonesopolis ConfirmPassword被添加到JSON中,它给出了相同的结果。当我调试并使用postman发布JSON时,它击中了ModelState断点。错误消息是字段是必需的。我不明白,因为我正在JSON请求中设置它们。那么
User
对象中是什么?我猜它是空的,可能需要
[FromBody]
post方法是视图控制器的一部分吗?如果没有,请发布完整的api?@AlexLeo它是api控制器(AccountsController)的一部分。。这个方法是我到目前为止为控制器写的唯一方法。
 public class User
    {
        public int Id { get; set; }
        [Required]
        public string FirstName { get; set; }
        [Required]
        public string LastName { get; set; }

        [Required]
        public string Email { get; set; }

        [Required]
        public string Password { get; set; }


        [NotMapped]
        [Compare("Password")]
        public string ConfirmPassword { get; set; }
        public int UserType { get; set; }
        public DateTime Date_Created { get; set; }
    }