title: ASP.NET MVC 5 : using Application Variable to create a Online User counter
body: <p>I come into a problem that probably is something dumb but I´ve just looked all over the places and just can´t make this work!</p>
  
  <p>I have a ASP.NET MVC web application created, and I want to use the Application["something"] to save a counter to mark visitors online. I had done a couple of times this process in ASP.NET , not in MVC...</p>
  
  <p>So, on Global.asax, I have the following code</p>
  
  <pre><code>public class MvcApplication : System.Web.HttpApplication
      {
          protected void Application_Start()
          {
          AreaRegistration.RegisterAllAreas();
          FilterConfig.RegisterGlobalFilters(GlobalFilters.Filters);
          RouteConfig.RegisterRoutes(RouteTable.Routes);
          BundleConfig.RegisterBundles(BundleTable.Bundles);
          HttpContext.Current.Application["totalVisitas"] = 0;
          HttpContext.Current.Application["visitasOnline"] = 0;
  }
  </code></pre>
  
  <p>And after that I created a Model to use this data and a Controller also with the following code:</p>
  
  <pre><code> public class OnlineStatsController : Controller
      {
          // GET: OnlineStats
          public ActionResult Index()
          {
  
          var modelOnline = new OnlineStatsViewModel
          {
              visitasOnline = (int)HttpContext.Application["visitasOnline"],
              totalVisitas = (int)HttpContext.Application["totalVisitas"]
  
          };
          return PartialView("UserStats",modelOnline);
      }
  }
  </code></pre>
  
  <p>ViewModel Code :</p>
  
  <pre><code>public class OnlineStatsViewModel
      {
          public int totalVisitas { get; set; }
  
          public int visitasOnline { get; set; }
  
  
      }
  }
  </code></pre>
  
  <p>Finally, I have a partial view with the following code:</p>
  
  <pre><code>@model  {namespace}.Models.OnlineStatsViewModel
  
  
  
      &lt;h3&gt;Visitas online: &lt;/h3&gt;
      &lt;p id="visitasOnline"&gt;@Model.visitasOnline&lt;/p&gt;
      &lt;h3&gt;Total de Visitas: &lt;/h3&gt;
      &lt;p id="totalVisitas"&gt;&lt;span class="badge"&gt;@Model.totalVisitas &lt;/span&gt;&lt;/p&gt;
  </code></pre>
  
  <p>And it is called by the layout.cshtml</p>
  
  <pre><code>&lt;body&gt;
      @Html.Partial("_topBar")
      &lt;div class="container body-content"&gt;
          @RenderBody()
          &lt;hr /&gt;
          &lt;footer&gt;
              &lt;div class="row"&gt;
                  &lt;div class="col-md-4"&gt;
                      &lt;p&gt;&amp;copy; @DateTime.Now.Year -&lt;/p&gt;
                  &lt;/div&gt;
                  &lt;div class="col-md-4"&gt;
                     @Html.Partial("UserStats",  new {namespace}.Models.OnlineStatsViewModel());
                  &lt;/div&gt;
              &lt;/div&gt;
          &lt;/footer&gt;
      &lt;/div&gt;
  
      @Scripts.Render("~/bundles/jquery")
      @Scripts.Render("~/bundles/bootstrap")
      @RenderSection("scripts", required: false)
  &lt;/body&gt;
  </code></pre>
  
  <p>All this, and everytime it load, it gives 0 users online and 0 historic. I even tried to hardcode the value in the controller making something like visitasOnline = 20000; but it only shows zero ... Sorry if the question is dumb, I´m tottaly newbie in MVC and trying a few courses online and got this problem =(</p>
  
  <p>EDIT --- Forgot to write where I update</p>
  
  <p>In global.asax I have the following also:</p>
  
  <pre><code> public void Session_Start(object sender, EventArgs e)
          {
              // Code that runs when a new session is started
              HttpContext.Current.Session["userName"] = "guest";
              HttpContext.Current.Application["visitasOnline"] = (int)HttpContext.Current.Application["visitasOnline"] +1;
              HttpContext.Current.Application["totalVisitas"] = (int)HttpContext.Current.Application["totalVisitas"] +1;
          }
  
          public 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.
              HttpContext.Current.Session["userName"] = "";
              HttpContext.Current.Application["visitasOnline"] = (int)HttpContext.Current.Application["visitasOnline"] - 1;
          }
      }
  </code></pre>