简体   繁体   中英

how to count number of visitors for website in asp.net c#

How do I count the number of visitors for website in asp.net c#?

I am using the code below:

In global.asax page:

<%@ Application Language="C#" %>

void Application_Start(object sender, EventArgs e)
{
    // Code that runs on application startup
    Application["NoOfVisitors"] = 0;
}

void Session_Start(object sender, EventArgs e)
{
    // Code that runs when a new session is started
    Application.Lock();
    Application["NoOfVisitors"] = (int)Application["NoOfVisitors"] + 1;
    Application.UnLock();
}

In .aspx page:

<asp:Label runat="server" ID="lbluser" />

In .aspx.cs:

protected void Page_Load(object sender, EventArgs e)
{
    lbluser.Text = Application["NoOfVisitors"].ToString();
}

The application counter is resetting to 0 every one hour ... Where have I erred in counting the number of users?

Application State is volatile. Check the this MSDN articule :

When using application state, you must be aware of the following important considerations:

  • ...

  • Volatility Because application state is stored in server memory, it is lost whenever the application is stopped or restarted. For example, if the Web.config file is changed, the application is restarted and all application state is lost unless application state values have been written to a non-volatile storage medium such as a database.

So you should not use that for saving this kind of data that you want to persist over time. Because applications pools get reseted from time to time. And I suspect you don't want to reset your visitor count when that happens.

You'll need some kind of data store which can persist your data when you application is not running.

Here are some choices:

In global.asax file under this method

void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
Application.Lock();
Application["NoOfVisitors"] = (int)Application["NoOfVisitors"] + 1;
Application.UnLock();
}

then in your page load please add

lblCount.Text = Application["NoOfVisitors"].ToString();

then you can get the number of visitors on your site .

If your application is hosted in IIS and has an application pool, you can check the Application Pool Recycling Settings . Depending on your version, the default is 1740 or 29 hours. Maybe the pool for your application is configured to 60 or around that value? The next setting to check is the Idle Time Out. I believe its default value is 20 on a new server. You can set this to 0. I recommend you read about these settings prior to changing them.

The only possible reason reason could be that, have you hosted your application on a third party server? if yes, it could be that the provider might be killing your application. i have numerous cases where these providers kill your application depending on their memory management schemes.

在将其值更改为数据库后简单地存储访问者计数,并在应用程序启动时从数据库加载此值,这就是您所要做的。

You should save count for visits on fly on an xml file under root directory. Check following blog for complete steps : How to count number of visitors in asp.net website

If you want to manage visitor on code level you need to start visitor counter under Application_Start method in application configuration file after need to increase the counter on every session. For more details follow the link given below.

 void Session_Start(object sender, EventArgs e) { // Code that runs when a new session is started Application.Lock(); Application["NoOfVisitors"] = (int)Application["NoOfVisitors"] + 1; Application.UnLock(); }

http://www.freshcodehub.com/Article/49/show-number-of-visitors-in-aspnet-application

Application Pool Restart periodically default settings is 60 minutes. when app pool restart the count restart as well.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM