网站总访问人数和当前在线人数

做网站肯定会要求实现这样的需要,相信对绝大部分程序员同行来说都很easy。其实只要我们对ASP.NET的内置对象的生命周期了解的话,很容易想到解决的办法。

在项目的根目录下新建global.asax文件,下面是我当年所写的代码:

ExpandedBlockStart.gif 代码
 1  void  Application_Start( object  sender, EventArgs e) 
 2      {
 3           //  在应用程序启动时运行的代码
 4          Application[ " onlines " =   1 ;
 5           try
 6          {
 7              Application[ " AccessCount " =  rs.GetDENetLinkSort( "" , " FType=1 " ).Rows[ 0 ][ " FAccessCount " ].ToString();
 8          }
 9           catch
10          {
11              Application[ " AccessCount " =   0 ;
12          }
13      }
14      
15       void  Application_End( object  sender, EventArgs e) 
16      {
17           //   在应用程序关闭时运行的代码
18           try
19          {
20               if  (Convert.ToInt32(Application[ " count " ])  <  Convert.ToInt32(Application[ " AccessCount " ]))
21              {
22                  SortedList sl  =   new  SortedList();
23                  sl.Add( " FID " " d49ad538-87ce-4a18-8b70-6d6dc5369b47 " );
24                  sl.Add( " FAccessCount " , Convert.ToInt32(Application[ " AccessCount " ]));
25                  rs.UpdateENetLinkSort(sl);
26              }
27               new  PBase().ExecuteSql( null " UPDATE Creadit_Site_AllVisit SET AllVisits= "   +  Convert.ToInt32(Application[ " AccessCount " ]), SqlResultEnum.No);
28          }
29          catch
30         {
31 
32         }
33           
34 
35      }
36          
37       void  Application_Error( object  sender, EventArgs e) 
38      { 
39           //  在出现未处理的错误时运行的代码
40 
41      }
42 
43       void  Session_Start( object  sender, EventArgs e) 
44      {
45           //  在新会话启动时运行的代码
46           this .Session.Timeout  =   20 ;
47          Application.Lock();
48          Application[ " onlines " =  Convert.ToInt32(Application[ " onlines " ])  +   1 ;
49          Application[ " AccessCount " =  Convert.ToInt32(Application[ " AccessCount " ])  +   1 ;
50           try
51          {
52 
53               int  count  =  rs.GetEENetLinkSort( "" " FType=1 " ).FAccessCount;
54               if  (count  <  Convert.ToInt32(Application[ " AccessCount " ]))
55              {
56                  SortedList sl  =   new  SortedList();
57                  sl.Add( " FID " " d49ad538-87ce-4a18-8b70-6d6dc5369b47 " );
58                  sl.Add( " FAccessCount " , Convert.ToInt32(Application[ " AccessCount " ]));
59                  rs.UpdateENetLinkSort(sl);
60              }
61               else
62              {
63                  Application[ " AccessCount " =  count;
64              }
65          }
66           catch
67          {
68 
69          }
70          Application.UnLock();
71      }
72 
73       void  Session_End( object  sender, EventArgs e) 
74      {
75           //  在会话结束时运行的代码。 
76           //  注意: 只有在 Web.config 文件中的 sessionstate 模式设置为
77           //  InProc 时,才会引发 Session_End 事件。如果会话模式设置为 StateServer 
78           //  或 SQLServer,则不会引发该事件。
79          Application.Lock();
80           if  (Convert.ToInt32(Application[ " onlines " ])  <=   0 )
81              Application[ " onlines " =   1 ;
82           else
83              Application[ " onlines " =  Convert.ToInt32(Application[ " onlines " ])  -   1 ;
84          Application.UnLock();
85      }
86         

 

可能有人会问在Application_End事件中为什么会有这样的代码: if (Convert.ToInt32(Application["count"]) < Convert.ToInt32(Application["AccessCount"]))?

这是因为当年我们在系统上线后,发现有时今天的总访问量可能会小于前天的总访问量,这个问题一直没找到原因,如有碰到此问题的朋友欢迎解答!

转载于:https://www.cnblogs.com/yangzhijia/archive/2010/07/26/1785254.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值