2

如何在应用程序变量中存储大约 100 个用户详细信息(用户名)?我知道如何使用会话,但我不知道如何使用应用程序变量以及如何存储在 global.asax 文件中?

4

2 回答 2

3

使用 Application 绝对像使用 Session。但是这个值是为所有用户共享的。

global.asax 中的更新:

void Application_Start(object sender, EventArgs e)
{
    var userList = new List<string>();
    Application["UserList"] = userList;
}
void Session_Start(object sender, EventArgs e)
{
    var userName = Membership.GetUser().UserName;

    List<string> userList;
    if(Application["UserList"]!=null)
    {
        userList = (List<string>)Application["UserList"];
    }
    else
        userList = new List<string>();
    userList.Add(userName);
    Application["UserList"] = userList;
}
于 2012-08-09T17:19:29.463 回答
2
var users = new List<string>(){
    "mary", "bob"
};
HttpContext.Current.Application["users"] = users;
于 2012-08-09T17:17:49.537 回答