그래서 저는 global.asax 이벤트 전체를 이해하려고합니다. 웹 사이트 방문을 기록하는 간단한 카운터를하고 있습니다. MSSQL을 사용하고 있습니다.global.asax와 그곳의 이벤트에 대해서
기본적으로 두 개의 정수가 있습니다. totalNumberOfUsers - 처음부터 총 visist입니다. currentNumberOfUsers - 현재 사이트를보고있는 총 사용자 수입니다.
그래서 global.asax 이벤트를 이해하는 방법은 누군가가 "Session_Start"사이트를 방문 할 때마다 한 번 해고하는 것입니다. 따라서 사용자 당 한 번. "Application_Start"는 누군가가 사이트를 처음 방문 할 때 한 번만 발생합니다.
여기에 내 global.asax 파일이 있습니다. 내가이 권리를 이해하는 경우를 Page_Load에서
<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>
내가 그래서이
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의 모든 포스트 백에서 증가하지만 파이어 폭스에서는 증가하지 않습니다.
그리고 마지막으로 한 가지는 동일한 것입니까?
Application["value"] = 0;
value = Application["value"]
//OR
Application.Set("Value", 0);
Value = Application.Get("Value");
이렇게하면 총 사용자 수가 줄어 듭니다. 현재 사용자가 실행중인 세션 수와 마찬가지로 사이트를 보는 것은 어떻습니까? 그 응용 프로그램 설정을 사용합니까? – eski