1
votes

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?

2
The APP_CODE folder is a location on disk. You need to make sure that you are accessing the session from the correct location in code. As in the correct class and method. Where are you accessing it from?Uatec
I am accessing this session on one of class file in APP_Code folder.Constant Learner
Which class and which method? and when is the method being called?Uatec
Check this.. that might be a duplicate question stackoverflow.com/questions/621549/…Philip Badilla
There appears to be a fundamental misunderstanding here of code execution order. APP_CODE is not a code site.Uatec

2 Answers

1
votes

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])
0
votes

In a class in app_code folder I have this :

public static class SessionTest
{
    public static string OutputSession()
    {
        return System.Web.HttpContext.Current.Session.SessionID.ToString();
    }
}

In a aspx.cs page I have this :

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<%
    Response.Write(SessionTest.OutputSession());
%>

It outputs the session id. Try this.