How to Count the number of times website visited and Online users in asp.net using C#.net?

This asp.net tutorial, we will discuss, how to count the number of times the website is visited by the visitors/users and the users currently online on the asp.net web site.

Visitors count in asp.net c#.net

We can tract this information by adding few lines of code inside the global.asax file.

First, it’s better to add a global.asax file. To add the global.asax file follow the below process

Right Click on your website -> Add NewItem -> Global Application Class. It will be added in the root directory of your website with the name “Global.asax”.

Open the global.aspx file and write the code on the Application_Start, Session_Start and the Session_End event as below:

void Application_Start(object sender, EventArgs e)
    {
        // Code that runs on application startup
        Application["SiteVisitedCounter"] = 0;
        //This will track how many users have currently opened our site write the following line
        Application["OnlineUserCounter"] = 0;
    }

 void Session_Start(object sender, EventArgs e)
    {
        // Code that runs when a new session is started
        Application.Lock();
        Application["SiteVisitedCounter"] = Convert.ToInt32(Application["SiteVisitedCounter"]) + 1;
        //This will track how many users have currently opened our site write the following line
        Application["OnlineUserCounter"] = Convert.ToInt32(Application["OnlineUserCounter"]) + 1;
        Application.UnLock();
    }

void Session_End(object sender, EventArgs e)
    {
        // Code that runs when a session ends.
        Application.Lock();
        Application["OnlineUserCounter"] = Convert.ToInt32(Application["OnlineUserCounter"]) – 1;
        Application.UnLock();
    }

Put the controls like below in the design page (default.aspx).

<div>
    <fieldset style ="width:320px;">
    <legend>Count site visited </legend>
        <asp:Label ID="lblSiteVisited" runat="server" Text=""
            ></asp:Label><br />
        <asp:Label ID="lblOnlineUsers" runat="server" Text=""
            ></asp:Label><br />
               <asp:Button ID="btnClearSesson" runat="server" Text="Clear My Session"
            onclick="btnClearSesson_Click" />
        </fieldset>
        </div>

In the code behind file(default.aspx.cs) write the code as below:

protected void Page_Load(object sender, EventArgs e)
    {
        lblSiteVisited.Text = "No of times my site visited=" + Application["SiteVisitedCounter"].ToString();
        lblOnlineUsers.Text = "No of users online on my site=" + Application["OnlineUserCounter"].ToString();
    }
    protected void btnClearSesson_Click(object sender, EventArgs e)
    {
        Session.Abandon();
    }

You may like following asp.net tutorials:

Now, when you the asp.net site, you will be able to see the number of times website visited and online users in asp.net using C#.net.

Donwload Hub site pdf

Download SharePoint Online Tutorial PDF FREE!

Get update on Webinars, video tutorials, training courses etc.

Bijay Kumar

I am Bijay from Odisha, India. Currently working in my own venture TSInfo Technologies in Bangalore, India. I am Microsoft Office Servers and Services (SharePoint) MVP (5 times). I works in SharePoint 2016/2013/2010, SharePoint Online Office 365 etc. Check out My MVP Profile.. I also run popular SharePoint web site SPGuides.com

>