所以我想了解的是整個global.asax事件。 我正在做一個記錄網站訪問的簡單計數器。我正在使用MSSQL。關於global.asax和那裏的事件
基本上我有兩個整數。 totalNumberOfUsers - 從開始的總visist。 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但當會議結束網站該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");
這會給我來的用戶總數。當前用戶查看該網站的情況如同正在運行多少個會話一樣。我會使用應用程序設置? – eski 2010-03-22 10:18:53