How can I access Session vars from base class in ASP.Net?

I ran into this issue regarding sessions in asp.net. I am creating an ASP.Net web application. I have created a class called BasePage that inherits from System.Web.Ui.Page. This BasePage class is System.Web.Ui.Page with an additional member called ActiveUser of type ActiveUser (a class I created myself). In the BasePage constructor, I am setting the ui member to this.ui = (ActiveUser)Session["ActiveUser"]

which is the previously set session var. However, when I run the project, I get HttpException

in the BasePage constructor at this.ui = Session["ActiveUser"]

. It tells me to check, however, however enableSessionState is set to true in the config file which I checked to have it. Does anyone have any ideas on how to solve this problem? That would be greatly appreciated. Thanx!

+2


source to share


1 answer


Curious why you keep this in the base page constructor?

You shouldn't be accessing the session from the constructor, but instead Page_Init. See the following post:

http://weblogs.asp.net/anasghanem/archive/2008/05/07/avoid-using-the-session-in-the-page-constructor.aspx



The session variable will be available anytime you implement the page functionality, so why not create a static class / method with functions to capture all of your session data? I don't understand why you want to duplicate this data's data in the base class.

You can check this thread: ASP.Net Session

+6


source







All Articles