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重启后就会重新从零进行统计。