【发布时间】:2011-08-06 03:02:09
【问题描述】:
我为我的应用程序的访问者数量(在线用户)编写了以下代码:
参考:
http://aspdotnetfaq.com/Faq/How-to-show-number-of-online-users-visitors-for-ASP-NET-website.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.SessionState;
using CardCharge.Classes;
namespace CardCharge
{
public class Global : System.Web.HttpApplication
{
protected void Application_Start(object sender, EventArgs e)
{
Application["OnlineUsers"] = 0;
}
protected void Session_Start(object sender, EventArgs e)
{
Application.Lock();
Application["OnlineUsers"] = (int)Application["OnlineUsers"] + 1;
Application.UnLock();
}
protected void Application_BeginRequest(object sender, EventArgs e)
{
}
protected void Application_AuthenticateRequest(object sender, EventArgs e)
{
}
protected void Application_Error(object sender, EventArgs e)
{
}
protected void Session_End(object sender, EventArgs e)
{
Application.Lock();
Application["OnlineUsers"] = (int)Application["OnlineUsers"] - 1;
Application.UnLock();
}
protected void Application_End(object sender, EventArgs e)
{
}
}
}
我还发现了以下使用 IHttpModule 的链接:
http://blog.sb2.fr/post/2008/12/01/HowTo-Get-Current-Online-Users-Count-and-Infos-with-ASPNET.aspx
我更喜欢使用简单的 global.asax 来实现我的目的,但这两种方式都太老了(3 年前)
.net 4 中有没有更好的方法来吸引在线用户(访问者数量)?
也为了获得一些计数(例如昨天),我需要数据库!
但是可以在 session_start 中连接 sql server 2008 还是在全局 asax 中连接 特别 session_end ?
提前致谢
【问题讨论】: