Home > Blockchain >  HttpPost returns null on list random initializer (asp .net)
HttpPost returns null on list random initializer (asp .net)

Time:03-15

Right now I'm working on asp net web api and I need to write post method which fill the list with random data.

here's the code

[HttpPost]
    public IActionResult ListsInitializer()
    {
        int mailNumber = _random.Next(3, 11);

        _users = new List<User>(mailNumber * 2);
        _mails = new List<Mail>(mailNumber);

        for (int i = 0; i < _users.Count; i  )
        {
            User tempUser = new User
            {
                UserName = _userNames[_random.Next(_userNames.Length - 1)],
                Email = GenerateEmail(_users)
            };
            
            _users.Add(tempUser);
        }

        for (int i = 0; i < _mails.Count; i  )
        {
            Mail tempMail = new Mail
            {
                Message = _messages[_random.Next(_messages.Length - 1)],
                ReceiverId = _users[_random.Next(_users.Count - 1)].Email,
                SenderId = _users[_random.Next(_users.Count - 1)].Email,
                Subject = _messages[_random.Next(_messages.Length - 1)]
            };
            
            _mails.Add(tempMail);
        }

        return Ok();
    }

I have no idea what I'm doing wrong but the method fills the list with null memebers and swagger returns [] empty array.

I'm sorry for asking such an easy question but I'm new in asp net and in webdev so I'm trying to figure out what is going on.

UPD: I'm so sorry but I also forgot to mention that the lists _users and _mails are static and still remains empty even after the .Add method. So if I call Get method or look inside the lists using debugger all the elements are null. I guess, the problem is not in return function. I cannot even fill the lists.

CodePudding user response:

[HttpPost]
public IActionResult ListsInitializer()
{
    int mailNumber = _random.Next(3, 11);

    _users = new List<User>(mailNumber * 2);
    _mails = new List<Mail>(mailNumber);

    for (int i = 0; i < _users.Count; i  )
    {
        User tempUser = new User
        {
            UserName = _userNames[_random.Next(_userNames.Length - 1)],
            Email = GenerateEmail(_users)
        };
        
        _users.Add(tempUser);
    }

    for (int i = 0; i < _mails.Count; i  )
    {
        Mail tempMail = new Mail
        {
            Message = _messages[_random.Next(_messages.Length - 1)],
            ReceiverId = _users[_random.Next(_users.Count - 1)].Email,
            SenderId = _users[_random.Next(_users.Count - 1)].Email,
            Subject = _messages[_random.Next(_messages.Length - 1)]
        };
        
        _mails.Add(tempMail);
    }
    
    //Solution

    //Return code
    Dictionary<string, string> data = new Dictionary<string, string>();
    data.Add("Users", JsonSerializer.Serialize(_users));
    data.Add("Mails", JsonSerializer.Serialize(_mails));
    
   //Now do it like
    return Ok(data);
}

CodePudding user response:

The problem was an inaccurate work with indexes. The list was initialised but empty so I have to use _users.Capacity instead of _users.Count

The right version of code is:

   for (int i = 0; i < _users.Capacity; i  )
    {
        User tempUser = new User
        {
            UserName = _userNames[_random.Next(_userNames.Length - 1)],
            Email = GenerateEmail(_users)
        };
        
        _users.Add(tempUser);
    }

    for (int i = 0; i < _mails.Capacity; i  )
    {
        Mail tempMail = new Mail
        {
            Message = _messages[_random.Next(_messages.Length - 1)],
            ReceiverId = _users[_random.Next(_users.Count - 1)].Email,
            SenderId = _users[_random.Next(_users.Count - 1)].Email,
            Subject = _messages[_random.Next(_messages.Length - 1)]
        };
        
        _mails.Add(tempMail);
    }
  • Related