count visits website MVC4 rasor
  • please how i can applying this code with MVC4 rasor thank you :) .

    void Application_Start(object sender, EventArgs e)
        {
            // Code that runs on application startup
            Application["SiteVisitedCounter"] = 0;
            //to check how many users have currently opened our site write the following line
            Application["OnlineUserCounter"] = 0;
        }

       void Session_Start(object sender, EventArgs e)
        {
            // Code that runs when a new session is started
            Application.Lock();
            Application["SiteVisitedCounter"] = Convert.ToInt32(Application["SiteVisitedCounter"]) + 1;
            //to check how many users have currently opened our site write the following line
            Application["OnlineUserCounter"] = Convert.ToInt32(Application["OnlineUserCounter"]) + 1;
            Application.UnLock();
        }

    void Session_End(object sender, EventArgs e)
        {
            // Code that runs when a session ends.
            // Note: The Session_End event is raised only when the sessionstate mode
            // is set to InProc in the Web.config file. If session mode is set to StateServer
            // or SQLServer, the event is not raised.
            Application.Lock();
            Application["OnlineUserCounter"] = Convert.ToInt32(Application["OnlineUserCounter"]) - 1;
            Application.UnLock();
        }
    In the design page (default.aspx) design the page as:
    HTML Source Code:

    <div>
        <fieldset style ="width:220px;">
        <legend>Count site visited and Online users</legend>   
            <asp:Label ID="lblSiteVisited" runat="server" Text=""
                style="color: #FFFFFF; background-color: #3366FF"></asp:Label><br />
            <asp:Label ID="lblOnlineUsers" runat="server" Text=""
                style="color: #FFFFFF; background-color: #009933"></asp:Label><br />
           
            <asp:Button ID="btnClearSesson" runat="server" Text="Clear Session"
                onclick="btnClearSesson_Click" />
            </fieldset>
            </div>

    C#.Net code
    In the code behind file(default.aspx.cs) write the code as:

    protected void Page_Load(object sender, EventArgs e)
        {
            lblSiteVisited.Text = "No of times site visited=" + Application["SiteVisitedCounter"].ToString();       
            lblOnlineUsers.Text = "No of users online on the site=" + Application["OnlineUserCounter"].ToString();
        }

        protected void btnClearSesson_Click(object sender, EventArgs e)
        {
            Session.Abandon();
        }

Howdy, Stranger!

It looks like you're new here. If you want to get involved, or you want to Ask a new Question, Please Login or Create a new Account by Clicking below

Login with Facebook

Popular Posts of the Week

Optimum Creative