will explain the code to get the total visitors website using Application variable.

this way depends on how many current sessions on your website.

So , First :

  • after Create an ASP.NET MVC website ,write the below code in Global.asax file.

  • In Application_Start() method declare the variable like below code.
  • Application["TotalofVisitor"] = 0;

  • Write the session_Start() method like below:

protected void Session_Start()  {        Application.Lock();        Application["TotalofVisitor"] = (int)Application["TotalofVisitor"] + 1;        Application.UnLock();  }  

Now to Your main Layout.cshtml or the View where you want to show the total count put the below code.

<p>Total Number of visitors: @ApplicationInstance.Application["TotalofVisitor"]</p>

 



Share

Comments