Introduction
This article will explain the code to get the total visitors count of Asp.Net MVC website using Application variable.
Step 1
- Create an Asp.Net MVC website and write the below code in Global.asax file.
- In Application_Start() method declare the variable like below code.
- Application["Totaluser"] = 0;
- Write the session_Start() method like below:
- protected void Session_Start()
- {
- Application.Lock();
- Application["Totaluser"] = (int)Application["Totaluser"] + 1;
- Application.UnLock();
- }
Step 2
If you want to show the total visitors count on all the pages then use Layout.cshtml or the page/view where you want to show the total count put the below code.
<p>Total Number of visitors: @ApplicationInstance.Application["Totaluser"]</p>