所以我想了解的是整个 global.asax 事件。 我做了一个简单的计数器来记录网站访问量。我正在使用 MSSQL。
基本上我有两个整数。 totalNumberOfUsers - 从一开始的总访问量。 currentNumberOfUsers - 当前查看网站的用户总数。
所以我对 global.asax 事件的理解是,每次有人访问该站点时,“Session_Start”都会被触发一次。所以每个用户一次。 “Application_Start”仅在有人第一次访问该站点时触发一次。
接下来我有我的 global.asax 文件。
<script runat="server">
string connectionstring = ConfigurationManager.ConnectionStrings["ConnectionString1"].ConnectionString;
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
Application.Lock();
Application["currentNumberOfUsers"] = 0;
Application.UnLock();
string sql = "Select c_hit from v_counter where (id=1)";
SqlConnection connect = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand(sql, connect);
cmd.Connection.Open();
cmd.ExecuteNonQuery();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Application.Lock();
Application["totalNumberOfUsers"] = reader.GetInt32(0);
Application.UnLock();
}
reader.Close();
cmd.Connection.Close();
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
Application.Lock();
Application["totalNumberOfUsers"] = (int)Application["totalNumberOfUsers"] + 1;
Application["currentNumberOfUsers"] = (int)Application["currentNumberOfUsers"] + 1;
Application.UnLock();
string sql = "UPDATE v_counter SET c_hit = @hit WHERE c_type = 'totalNumberOfUsers'";
SqlConnection connect = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand(sql, connect);
SqlParameter hit = new SqlParameter("@hit", SqlDbType.Int);
hit.Value = Application["totalNumberOfUsers"];
cmd.Parameters.Add(hit);
cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
Application.Lock();
Application["currentNumberOfUsers"] = (int)Application["currentNumberOfUsers"] - 1;
Application.UnLock();
}
</script>
在 page_load 我有这个
protected void Page_Load(object sender, EventArgs e)
{
l_current.Text = Application["currentNumberOfUsers"].ToString();
l_total.Text = Application["totalNumberOfUsers"].ToString();
}
因此,如果我没有理解错的话,每次有人访问该站点时,currentNumberOfUsers 和 totalNumberOfUsers 都会增加 1。但是当 session 结束时,currentNumberOfUsers 会减少 1。
如果我使用同一台计算机使用 3 种浏览器访问该站点,我应该在两个柜台上获得 3 次点击。几个小时后再次执行此操作,我应该有 3 个当前和 6 个,对吧?
它现在的工作方式是当前增加到 2,并且在 IE 和 Chrome 上每次回发时总数都会增加,但在 Firefox 上不会。
最后一件事,这是同一件事吗?
Application["value"] = 0;
value = Application["value"]
//OR
Application.Set("Value", 0);
Value = Application.Get("Value");
最佳答案
假设您使用的是标准的 Asp.net 成员(member)提供程序,您将能够使用 membershipProvider.GetNumberOfUsersOnline
如果您希望实现自定义解决方案,您可能想要创建一个自定义 MembershipProvider 并遵循 Microsoft 的实现方式。它基于所有用户的数量 LastActivityDate大于当前日期减去 UserIsOnlineTimeWindow
关于c# - 关于 global.asax 和那里的事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2491179/