做网站肯定会要求实现这样的需要,相信对绝大部分程序员同行来说都很easy。其实只要我们对ASP.NET的内置对象的生命周期了解的话,很容易想到解决的办法。
在项目的根目录下新建global.asax文件,下面是我当年所写的代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![ExpandedBlockStart.gif](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.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
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"]))?
这是因为当年我们在系统上线后,发现有时今天的总访问量可能会小于前天的总访问量,这个问题一直没找到原因,如有碰到此问题的朋友欢迎解答!