1.新建網站,添加幾個窗體。webForm1.aspx ,ViewStateForm.aspx
2.在網站的根目錄下添加全局應用程序類“Global.aspx” 。(重要)
3.在“Global.aspx” 有固有的格式和會話信息結構。
4.在“Global.aspx”中各個函數中添加處理代碼。詳細如下:
<%@ Application Language="C#" %>
<script runat="server">
void Application_Start(object sender, EventArgs e) //初始化站點的在線人數
{
// 在應用程序啓動時運行的代碼//初始化變量:UserCount 和 StatCount
Application.Lock(); //臨界變量,使用加鎖功能,其他用戶不能訪問。
Application["UserCount"] = 0;
Application.UnLock(); //臨界變量被解鎖。
Application.Lock(); //臨界變量,使用加鎖功能,其他用戶不能訪問。
Application["StatCount"] = 0;
Application.UnLock(); //臨界變量被解鎖。
Application.Lock(); //臨界變量,使用加鎖功能,其他用戶不能訪問。
Application["StatCount_ViewSF"] = 0;
Application.UnLock(); //臨界變量被解鎖。
}
void Application_End(object sender, EventArgs e)
{
// 在應用程序關閉時運行的代碼
}
void Application_Error(object sender, EventArgs e)
{
// 在出現未處理的錯誤時運行的代碼
}
void Session_Start(object sender, EventArgs e) //站點在線人數加一
{
// 在新會話啓動時運行的代碼
Application.Lock(); //臨界變量,使用加鎖功能,其他用戶不能訪問。
Application["UserCount"] = Int32.Parse(Application["UserCount"].ToString()) + 1;
Application.UnLock(); //臨界變量被解鎖。
//測試某一頁的訪問量※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※※
String pageurl = Request.Url.ToString();//獲取用戶訪問的頁面
if(pageurl .EndsWith ("WebForm1.aspx")) //判斷訪問的是否是默認頁
{
//鎖定變量
Application.Lock();
//頁面訪問量加一
Application["StatCount"] = int.Parse(Application["StatCount"].ToString()) + 1;
//解鎖
Application.UnLock();
}
else if (pageurl.EndsWith("ViewStateForm.aspx")) //判斷訪問的是否是默認頁
{
//鎖定變量
Application.Lock();
//頁面訪問量加一
Application["StatCount_ViewSF"] = int.Parse(Application["StatCount_ViewSF"].ToString()) + 1;
//解鎖
Application.UnLock();
}
}
void Session_End(object sender, EventArgs e) //站點在線人數減一
{
// 在會話結束時運行的代碼。
// 注意: 只有在 Web.config 文件中的 sessionstate 模式設置爲
// InProc 時,纔會引發 Session_End 事件。如果會話模式設置爲 StateServer
// 或 SQLServer,則不會引發該事件。
Application.Lock();
Application["UserCount"] = Int32.Parse(Application["UserCount"].ToString()) - 1;
Application.UnLock();
}
//Http請求開始和結束時的處理事件
protected void Application_BeginRequest(object sender, EventArgs e)
{
//取得表的TabID
//int tabId = 0; int tabIndex = 0;
//if(Request .Params ["TabId"]!=null)
//{
// tabId = Int32.Parse(Request .Params ["TabId"]);
//}
//if(Request .Params ["tabIndex"]!=null )
//{
// tabIndex = Int32.Parse(Request .Params ["TabIndex"]);
//}
}
protected void Application_EndRequest(object sender, EventArgs e)
{
}
//Http請求驗證的處理事件
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
}
</script>
5. 在webForm1.aspx 的相應的CS文件中添加如下的代碼:
public partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) { OutputUserCount(); }
}
protected void OutputUserCount() //顯示當前站點在線人數
{
Response.Write("站點在線人數:");
Response.Write(Application["UserCount"].ToString());
Response.Write(" 人。");
Response.Write("本頁面的訪問量:");
Response.Write(Application["StatCount"].ToString());
Response.Write(" 。");
}
}
6. ViewStateForm.aspx 的相應的CS文件中添加如下的代碼:
public partial class ViewStateForm : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack) { OutputUserCount(); }
}
protected void OutputUserCount() //顯示當前站點在線人數
{
Response.Write("站點在線人數:");
Response.Write(Application["UserCount"].ToString());
Response.Write(" 人。");
Response.Write("本頁面的訪問量:");
Response.Write(Application["StatCount_ViewSF"].ToString());
Response.Write(" 。");
}
}
7. webconfig 中也有部分對session的配置控制。
<sessionState mode="InProc"
cookieless="true"
timeout="20" />
<!--
會話狀態設置
默認狀態下,asp.net 使用 cookie 標示哪些請求屬於特定的會話。
如果cookie 不可用,則可以通過將會話標識符添加到url,來跟蹤會話。
若要禁用cookie ,請設置sessionstate cookieless="true"。
首次使用了:<sessionState
mode="InProc"
stateConnectionString="tcpip=127.0.0.1:42424"
sqlConnectionString="data source= 127.0.0.1;userid=sa;password="
cookieless="false"
timeout="20"
/>
然後就可以在IIS中進行測試了。這個處理方法在IIS重啓後就會重新從零進行統計。