C# – How to access session variable in APP_CODE class file

asp.netc

We have ASP.Net application.
On Normal aspx.cs page I can access session like this,

 Convert.ToString(Session[Resources.AMS.SESSION_USERID]);

But in APP_CODE folder, When i try, I can't get session value, it gives NULL!!

Convert.ToInt32(System.Web.HttpContext.Current.Session[Resources.AMS.SESSION_USERID])

What need to done?

Best Answer

Only the page will have Web Context values set.

App_Code is where you can store your class files, see here. Class is a template for an object. You only give value to it during run time.

What you can do is add a property to your class.

private int _sessionID;
public int SessionID
{
    get
    {
        return _sessionID;
    }
    set
    {
        sessionID = value;
    }
}

Then in your code behind, (aspx.cs)

var myClass = new myClass();

myClass.SessionID = Convert.ToInt32(System.Web.HttpContext.Current.Session[Resources.AMS.SESSION_USERID])