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

被刻印的时光 ゝ 提交于 2019-12-24 00:53:47

问题


I've run in to a this problem regarding Sessions in asp.net. I'm creating an ASP.Net web application. I've created a class called BasePage that inherits from System.Web.Ui.Page. This BasePage class is a System.Web.Ui.Page with an additional member called ActiveUser of type ActiveUser (a class I've created of my own). In the constructor of BasePage I set the member ui to this.ui = (ActiveUser)Session["ActiveUser"], which is a session var that is previously set. However, when I run my project i get a HttpException in BasePage's constructor on this.ui = Session["ActiveUser"]. It tells me to check however enableSessionState is set to true in the config file, which I've checked that it is. Do anyone have any idéas on how to solve this one? It would be very much appreciated. Thanx!


回答1:


Just curious why are you saving this in the base page constructor?

You shouldn't be accessing the session from the constructor but the Page_Init instead. 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 accessible at any time when implementing the page functionality so why not create a static class / method with functionality to grab all your session data? I don't see why you would want to duplicate storage of this data in your base class.

You might want to check out this thread: ASP.Net Session



来源:https://stackoverflow.com/questions/1516553/how-can-i-access-session-vars-from-base-class-in-asp-net

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!