Manraj
Manraj

Reputation: 494

How to count the number of times a website is visited and the number of online users in MVC

I need to find out the number of times a website has been visited and how many online users it has.

My code is this:

Global.asax

 protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);


        // 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();
    }

The HomeController class contains the following code. I got an error on System.Net.Mime.MediaTypeNames.Application.

   [HttpGet]
   public ActionResult Index()
    {
        ViewBag.noofsitesvisited = "No of times site visited=" + System.Net.Mime.MediaTypeNames.Application["SiteVisitedCounter"].ToString();
        ViewBag.onlineusers = "No of users online on the site=" + System.Net.Mime.MediaTypeNames.Application["OnlineUserCounter"].ToString();
    }

Upvotes: 0

Views: 2597

Answers (2)

Chris Pratt
Chris Pratt

Reputation: 239260

You don't want to do it this way. One, reading and writing data from anything global in web environment is dangerous and inadvisable from the get go, and two, this will only store the count while the AppPool is active anyways. If the server restarts or the AppPool restarts or even just recycles, your counts all go away and you start over from zero.

If you want to store a count that needs to persist, then you need to use a persistent medium: database, text file, etc. Not only is this safer in general, it is also the only way to have a true persistent count.

That said, why not just use Google Analytics or some other form of website analytics. Not only are you reinventing the wheel, but actual analytics tracking will be more accurate and provide more useful statistics than anything you can do on your own.

Upvotes: 1

user449689
user449689

Reputation: 3154

You need to change the code in the controller as follows:

   [HttpGet]
    public ActionResult Index()
    {
        ViewBag.noofsitesvisited = "No of times site visited=" + HttpContext.Application["SiteVisitedCounter"].ToString();
        ViewBag.onlineusers = "No of users online on the site=" + HttpContext.Application["OnlineUserCounter"].ToString();
    }

In MVC Application variables are accessible via HttpContext

Upvotes: 1

Related Questions