Empty session in a 'CS' file

Last post 03-31-2008 2:46 AM by Benson Yu - MSFT. 7 replies.

Sort Posts:

  • Empty session in a 'CS' file

    03-27-2008, 5:05 AM
    • Loading...
    • danal
    • Joined on 04-29-2004, 8:31 AM
    • Posts 70

    Hello,

    I am writing an ASP.NET c# application.
    I am assiging a session variable on an 'aspx' page, when the application starts:
    Session["sessionid"] = Session.SessionID;

    Then, I want to read it in a 'cs' file:
    HttpContext.Current.Session["sessionid"].ToString()

    But, HttpContext.Current is null.

    I also tried: HttpContext.Current.Session.SessionID because what i need is the sessionID but, here too, HttpContext.Current is null.

    How can i get the session value or the session id in the cs file?

    Thanks,

    Dana

  • Re: Empty session in a 'CS' file

    03-27-2008, 8:42 AM
    • Loading...
    • johram
    • Joined on 06-13-2006, 6:36 AM
    • Sweden
    • Posts 2,814
    • Moderator

    You haven't disabled session or any other important http module in you web config?

    If this post was useful to you, please mark it as answer. Thank you!
  • Re: Empty session in a 'CS' file

    03-27-2008, 9:06 AM
    • Loading...
    • ely79
    • Joined on 03-17-2008, 1:56 PM
    • Denver, Colorado
    • Posts 187

    Where is the .cs file?  Is it a code behind for a page, in the app_code directory, another project?  You should be able to access HttpContext.Current most anywhere in your web application.

  • Re: Empty session in a 'CS' file

    03-27-2008, 9:11 AM
    • Loading...
    • johram
    • Joined on 06-13-2006, 6:36 AM
    • Sweden
    • Posts 2,814
    • Moderator

    I'm not sure you're able to access it from within an external assembly, but if the class i placed in App_Code then it should work.

    If this post was useful to you, please mark it as answer. Thank you!
  • Re: Empty session in a 'CS' file

    03-27-2008, 10:42 AM
    • Loading...
    • naimulah
    • Joined on 02-23-2008, 8:55 PM
    • Posts 25

    Then, I want to read it in a 'cs' file:
    HttpContext.Current.Session["sessionid"].ToString()

    ....... it seems like you are trying to access the HttpContext object from a assembly and not within the code in the web application. I am not sure if you can access this object from anywhere else except from the code sitting in its APP_CODE directory.

    If you do need it; then probably its best to pass along the Page.Context as an argument to the function and use it from there.

     

  • Re: Empty session in a 'CS' file

    03-29-2008, 1:59 PM
    • Loading...
    • danal
    • Joined on 04-29-2004, 8:31 AM
    • Posts 70

    No, I haven't disabled the session in my web.config

  • Re: Empty session in a 'CS' file

    03-29-2008, 2:02 PM
    • Loading...
    • danal
    • Joined on 04-29-2004, 8:31 AM
    • Posts 70

    The class is located in the app_code directory of my project.

  • Re: Empty session in a 'CS' file

    03-31-2008, 2:46 AM
    Answer

    Hi danal,

    In my opinion, it depends on how you invoke the corresponding method which defined in the separate class file. If the invoked method is running in the same thread of Page_Load, the HttpContext.Current shouldn’t be null. For example, the following code works on my machine.

        protected void Page_Load(object sender, EventArgs e)
        {
            Session["sessionid"] = "01233210";
            Class1 c1 = new Class1();
            Response.Write(c1.GetSessionID());
        }

    ***************** App_Code\Class1.cs *****************
            public string GetSessionID()
            {
                return HttpContext.Current.Session["sessionid"].ToString();
            }

    If that method is running in the different thread, you should pass a reference like “naimulah” said. For more informatin, please refer to the following link. Hope it is helpful to you.

    Working With HttpContext.Current
    http://www.odetocode.com/Articles/112.aspx

    Sincerely,
    Benson Yu
    Microsoft Online Community Support

    Please remember to mark the replies as answers if they help and unmark them if they provide no help. This can be beneficial to other community members reading the thread.
Page 1 of 1 (8 items)