How to Count Total Number of HIT in Asp.net MVC?

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:
      1. protected void Session_Start()  
      2. {  
      3.       Application.Lock();  
      4.       Application["Totaluser"] = (int)Application["Totaluser"] + 1;  
      5.       Application.UnLock();  
      6. }  
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>
Ebook Download
View all
Learn
View all