Using Application State to Check User Visits on a Website

This article shows how to make a user's total number of visits to a website using application state in ASP.Net using C#, we also use a global.asax file here.

The Global.asax file is the ASP.Net application file that contains the code for application state level events raised by the ASP.Net or HTTP modules. Global.asax resides in the root directory of ASP.Net.

INITIAL CHAMBER

Step 1

Open your Visual Studio 2010 and create an Empty Website, provide a suitable name (Applicationstate_demo).

Step 2

In Solution Explorer you get your empty website, then add a Web Form and Global.asax file. By going as in the following.

For Web Form

applicationstate_demo (your empty website) then right-click then select Add New Item -> Web Form. Name it applicationstate.aspx.

For Global.asax

applicationstate_demo (your empty website) then right-click then select Add New Item -> Global Application Class.


Figure 1: Global Class

Global.asax Code

  1. <%@ Application Language="C#" %>  
  2. <script runat="server">  
  3. void Application_Start(object sender, EventArgs e)   
  4. {  
  5. // Code that runs on application startup  
  6. Application["VisitorCount"] = 0;  
  7. }  
  8. void Application_End(object sender, EventArgs e)   
  9. {  
  10. // Code that runs on application shutdown  
  11. }  
  12. void Application_Error(object sender, EventArgs e)   
  13. {   
  14. // Code that runs when an unhandled error occurs  
  15. }  
  16. void Session_Start(object sender, EventArgs e)   
  17. {  
  18. // Code that runs when a new session is started  
  19. Application["VisitorCount"] = (int)Application["VisitorCount"] + 1;  
  20. }  
  21. void Session_End(object sender, EventArgs e)   
  22. {  
  23. // Code that runs when a session ends.   
  24. // Note: The Session_End event is raised only when the sessionstate mode  
  25. // is set to InProc in the Web.config file. If session mode is set to StateServer   
  26. // or SQLServer, the event is not raised.  
  27. }  
  28. </script>
As the application starts we have visitor count = 0. As we proceed we increment the visitor count to +1.


Figure 2: Code

As the session starts we set the visitor count to +1 in the event of the session start.


Figure 3: Session Start

This is my welcome page. You can make your own or you can just make it blank and print the label for the total of visits.
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>  
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  3. <html  
  4.     xmlns="http://www.w3.org/1999/xhtml">  
  5.     <head runat="server">  
  6.         <title></title>  
  7.         <style type="text/css">  
  8. .style1  
  9. {  
  10. text-align: center;  
  11. }  
  12. .style2  
  13. {  
  14. font-size: xx-large;  
  15. }  
  16. </style>  
  17.     </head>  
  18.     <body bgcolor="#ffffcc">  
  19.         <form id="form1" runat="server">  
  20.             <div class="style1">  
  21.                 <strong>  
  22.                     <span class="style2">Welcome to Active DotNet  
  23.                         <br />  
  24.                         <br />  
  25.                     </span>  
  26.                 </strong>  
  27.                 <br />  
  28.                 <br />  
  29.                 <br />  
  30.                 <br />  
  31.                 <asp:Image ID="Image1" runat="server" Height="350px" ImageUrl="~/1.jpg" />  
  32.                 <br />  
  33.                 <br />  
  34.             </div>  
  35.             <p>    
  36.                 <asp:Label ID="Label2" runat="server"   
  37.                   style="text-align: center; font-weight: 700; font-size: large"></asp:Label>  
  38.                </p>  
  39.         </form>  
  40.     </body>  
  41. </html>

Figure 4: Output 1

Here in the label we will print the total visiting. Look the following image.


Figure 5: Output 2


Figure 6: Output 3

I hope you like this. Have a nice day. Thank you for reading.

Up Next
    Ebook Download
    View all
    FileInfo in C#
    Read by 9.7k people
    Download Now!
    Learn
    View all