locked
Application("strVisitors") RRS feed

  • Question

  • User-1753299103 posted

    What is the code for counting visitors in Razor syntax.

    This dosen't work:

    Application("strVisitors")

    Thursday, May 9, 2013 11:13 AM

Answers

  • User-821857111 posted

    That's a classic ASP global.asa file. It won't work in an ASP.NET application. You need to add a C# global.asax file then modify it like this:

    <%@ Application Language="C#" %>
    <script runat="server">
    
        void Application_Start(object sender, EventArgs e) 
        {
            Application["CurrentUsers"] = 0;
    
        }
        
        void Application_End(object sender, EventArgs e) 
        {
            //  Code that runs on application shutdown
    
        }
            
        void Application_Error(object sender, EventArgs e) 
        { 
            // Code that runs when an unhandled error occurs
    
        }
    
        void Session_Start(object sender, EventArgs e) 
        {
            Application.Lock();
            Application["CurrentUsers"] = System.Convert.ToInt32(Application["CurrentUsers"]) + 1;
            Application.UnLock();
        }
    
        void Session_End(object sender, EventArgs e) 
        {
            Application.Lock();
            Application["CurrentUsers"] = System.Convert.ToInt32(Application["CurrentUsers"]) - 1;
            Application.UnLock();
    
        }
           
    </script>
    

    Then you can reference the application variable in your pages like this:

    <p>Current Visitors: @HttpContext.Current.Application["CurrentUsers"]</p>




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 9, 2013 5:26 PM

All replies

  • User197322208 posted

    This dosen't work:

    Application("strVisitors")

    What have you put in

    Application("strVisitors")

    ?

     

    Thursday, May 9, 2013 11:14 AM
  • User-1753299103 posted

    @Application("strVisitors")

    Error: The name 'Application' does not exist in the current context

    Thursday, May 9, 2013 11:34 AM
  • User-821857111 posted

    You can store global variables in Razor Web Pages in the special AppState object:

    AppState["strVisitors"]

    Or you can use the dynamic version:

    App.Visitors

    http://msdn.microsoft.com/en-us/library/system.web.webpages.helperpage.appstate(v=vs.111).aspx

    Thursday, May 9, 2013 1:45 PM
  • User-1753299103 posted

    Should either one of these 2 display a number?

    Because they don't!

    @App.Visitors
    
    @AppState["strVisitors"];
    Thursday, May 9, 2013 2:10 PM
  • User-821857111 posted

    They will only display a number if you set their value to a number. They are simply global variables. There's nothing within the framework that automatically stores the number of visitors. You need to write code to do that.

    Thursday, May 9, 2013 3:43 PM
  • User-1753299103 posted

    I have the code in my global.asa

     

    <SCRIPT LANGUAGE="VBScript" RUNAT="Server"> 
    Sub Application_OnStart 
    Application("strVisitors") = 0 
    End Sub 
    
    Sub Application_OnEnd 
    End Sub 
    
    Sub Session_OnStart 
    Application.lock 
    Application("strVisitors")=Application("strVisitors") + 1 
    Application.unlock 
    End Sub 
    
    Sub Session_OnEnd 
    Application.lock 
    Application("strVisitors")=Application("strVisitors") - 1 
    Application.unlock 
    End Sub 
    </SCRIPT>

    Thursday, May 9, 2013 4:30 PM
  • User-821857111 posted

    That's a classic ASP global.asa file. It won't work in an ASP.NET application. You need to add a C# global.asax file then modify it like this:

    <%@ Application Language="C#" %>
    <script runat="server">
    
        void Application_Start(object sender, EventArgs e) 
        {
            Application["CurrentUsers"] = 0;
    
        }
        
        void Application_End(object sender, EventArgs e) 
        {
            //  Code that runs on application shutdown
    
        }
            
        void Application_Error(object sender, EventArgs e) 
        { 
            // Code that runs when an unhandled error occurs
    
        }
    
        void Session_Start(object sender, EventArgs e) 
        {
            Application.Lock();
            Application["CurrentUsers"] = System.Convert.ToInt32(Application["CurrentUsers"]) + 1;
            Application.UnLock();
        }
    
        void Session_End(object sender, EventArgs e) 
        {
            Application.Lock();
            Application["CurrentUsers"] = System.Convert.ToInt32(Application["CurrentUsers"]) - 1;
            Application.UnLock();
    
        }
           
    </script>
    

    Then you can reference the application variable in your pages like this:

    <p>Current Visitors: @HttpContext.Current.Application["CurrentUsers"]</p>




    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, May 9, 2013 5:26 PM