列表随机初始化程序 (asp .net) 上的 HttpPost returns null

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

现在我正在开发 asp 网络 api,我需要编写 post 方法来用随机数据填充列表。

这是代码

[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();
    }

我不知道我做错了什么,但该方法用空成员和 swagger 填充列表 returns [] 空数组。

很抱歉问了这么简单的问题,但我是 asp 网络和 webdev 的新手,所以我想弄清楚发生了什么。

更新:我很抱歉,但我也忘了提到列表 _users 和 _mails 是静态的,即使在 .Add 方法之后仍然是空的。因此,如果我调用 Get 方法或使用调试器查看列表内部,所有元素都为空。我想,问题不在 return 函数中。我什至无法填写列表。

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

问题是对索引的处理不准确。 该列表已初始化但为空,因此我必须使用 _users.Capacity 而不是 _users.Count

正确的代码版本是:

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