title: Maintaining session in a class C#
body: <p>I am trying to maintain session in a class for <strong>MVC application</strong>. Below is the code which i used get and set session in a variable. But whenever i am accessing this variable after set, it is giving null.</p>
  
  <pre><code>public static class MySession
  {
      public static string MyVar
      {
          get
          {
              return HttpContext.Current.Session["MyVar"] == null ? "" : HttpContext.Current.Session["MyVar"].ToString();
          }
          set
          {
              HttpContext.Current.Session["MyVar"] = value;
          }
      }
  }
  </code></pre>
  
  <p>And I used to set as
  <code>MySession.MyVar="testData";</code></p>
  
  <p>But when i access
  <code>string str = MySession.MyVar;</code></p>
  
  <p>it gives <code>null</code> value. Does anybody know why my session is not stored?</p>