Session is null when inheriting from System.Web.UI.Page

I want to extend the System.Web.UI.Page class with some additional material. In ctor, I need the value of the session variable.

The problem is that the Session object is null ...

public class ExtendedPage : System.Web.UI.Page {
   protected foo;   

   public ExtendedPage() {
      this.foo = (int)HttpContext.Current.Session["foo"];   // NullReferenceException
   }
}

If I move the part with the session object to the Load-Event, everything works fine ...

public class ExtendedPage : System.Web.UI.Page {
   protected foo;

   public ExtendedPage() {
      this.Load += new EventHandler(ExtendedPage_Load);
   }

   void ExtendedPage_Load(object sender, EventArgs e) {
      this.foo = (int)HttpContext.Current.Session["foo"];
   }
}

Why is the Session object null in the first case?

+3
source share
2 answers

The property is Sessionset later in the life cycle of the page after creating the object Page.

+5
source

You must inherit the ExtendedPage class on the .aspx page.

Session HttpContext

0

Source: https://habr.com/ru/post/1750308/


All Articles