1

I am trying to find number of visitors for a site using Application.

My Global.asax.cs looks like this:

public class MvcApplication : System.Web.HttpApplication
{
    protected void Application_Start()
    {
        AreaRegistration.RegisterAllAreas();
        FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
        RouteConfig.RegisterRoutes(RouteTable.Routes);
        BundleConfig.RegisterBundles(BundleTable.Bundles);

        //Added for version tracking
        Application["ActiveUsers"] = 0;
    }

And my Controller I added following code:

string i =  Application["ActiveUsers"].ToString();

But I am getting a error "Application' does not exist in the current context" .

Am I missing any namespace or is Application doesn't work in Controller level.(I don't see error in Golbal.asax.cs)

Thanks.

Santosh
  • 2,355
  • 10
  • 41
  • 64
  • It's `MvcApplication` and I don't think you can even do that. – Steve Jan 30 '20 at 15:55
  • This would not work on a load balanced server, and remember that IIS will recycle your domain every 28 hours or so (sometimes even more frequently). You should be storing value this in a database. – Neil Jan 30 '20 at 15:56

1 Answers1

1

You can access that via:

int i = HttpContext.Application["ActiveUsers"] as int;
mxmissile
  • 11,464
  • 3
  • 53
  • 79