我有一种方法,它使用 .NET 成员资格提供程序来使用配置文件提供程序为每个用户检索一些数据。但是我在我的应用程序日志中收到一个错误,指出
方法中
GetUsers():
的索引超出了数组的范围。
“in 方法GetUsers()
”是由我的日志记录机制添加的。其余的是严格的.NET。
protected Dictionary<string, string> Getusers()
{
try
{
Dictionary<string, string> userDictionary = new Dictionary<string, string>();
//Get string array of users in role from RoleProvider
var users = Roles.GetUsersInRole("MyRoleName");
//loop through the array of usernames
foreach (string user in users)
{
//Get user's first name and last name from the profile provider
ProfileCommon profile = (ProfileCommon)ProfileCommon.Create(user);
//add new key value pair with user name as the key and lastname,firstname as value
userDictionary.Add(user, string.Concat(profile.LastName + ",", profile.FirstName));
}
//sort the dictionary by the value not the key
//a little linq to do the sorting
var sortedDict = (from entry in userDictionary orderby entry.Value ascending select entry).ToDictionary(pair => pair.Key, pair => pair.Value);
return sortedDict;
}
catch (Exception ex)
{
LogController.LogError( "In Method -- Getusers() --" + ex.Message);
//if error occurs return empty dictionary
return new Dictionary<string, string>();
}
}