这是我用于计算在线用户的代码,它在 global.asax 中运行良好,并且计算良好,但是当我从控制器调用它时,它返回 null
void Session_Start(object sender, EventArgs e)
{
Application.Lock();
Application["OnlineUsers"] = (int)Application["OnlineUsers"] + 1;
Application.UnLock();
}
void Session_End(object sender, EventArgs e)
{
Application.Lock();
Application["OnlineUsers"] = (int)Application["OnlineUsers"] - 1;
Application.UnLock();
}
protected void Application_Start()
{
Application["OnlineUsers"] = 0;
}
在控制器中:
string strUsersOnline = HttpContext.Application["OnlineUsers"] as string;
ViewData["OnlineUsers"] = strUsersOnline;