首页 > 解决方案 > 在反应和 Web-API 项目之间连接 - 对象以 null 和 0 到达服务器

问题描述

我有一个用 React 和 web API-C# 编写的项目,
当我发送一个带有数据的发布请求时,它涉及用 null 和 0 初始化的控制器

在反应中设置对象:

class user {
    UserID
    FirstName
    LastName
    SMS
    Email
    Phone
    RoleID
    UserName
    Password
    Dock
    DocName
    status

    constructor(userID = null, FirstName = null, LastName = null, SMS = null, Email = null, Phone = null, RoleID = null, userName = null, Password = null, document = null, docName = null, status = true) {
        this.UserID = userID
        this.FirstName = FirstName
        this.LastName = LastName
        this.SMS = SMS
        this.Email = Email
        this.Phone = Phone
        this.RoleID = RoleID
        this.UserName = userName
        this.Password = Password
        this.Dock = document
        this.DocName = docName
        this.status = status
    }

}
export default user

在 C# 中:

public class UserDTO
    {
        public int UserID { get; set; }
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string SMS { get; set; }
        public string Email { get; set; }
        public string Phone { get; set; }
        public int RoleID { get; set; }
        public string UserName { get; set; }
        public string Password { get; set; }
        public string Dock { get; set; }
        public string DocName { get; set; }
        public string status { get; set; }
        public UserDTO()
        {

        }
}

发送到服务器:

const use1 = new UserObject(1, 'aa', 'bb', null, null, null, 3);
    return (
      <div>
        <button onClick={() => { Axios.post('https://localhost:44368/api/User/AddUser', JSON.stringify((use1)),).then(res => { console.log(res.data) }) }} >post with object</button>
         </div>

    )

在客户端它已满, 截图 1

在服务器中, 截图 2

标签: reactjsasp.net-web-api

解决方案


推荐阅读