Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Session state is not available in this context

Tags:

I want to read session id in application error event but always get error "Session state is not available in this context". Why? The strange thing is that I have the same code in another asp.net app and everything works fine.

void Application_Error(object sender, EventArgs e) {          var sessionId = Session.SessionID;         //skipped code  } 
like image 986
Tomas Avatar asked Aug 04 '11 11:08

Tomas


People also ask

How do I turn on session state?

To enable in-process session state by using the UIOpen IIS Manager and navigate to the level you want to manage. In Features View, double-click Session State. On the Session State page, in the Session State Mode Settings area, click In process.

What is session and state in this context?

Session state is an ASP.NET Core scenario for storage of user data while the user browses a web app. Session state uses a store maintained by the app to persist data across requests from a client. The session data is backed by a cache and considered ephemeral data.

How do you turn session state for a Web form?

web configuration section. You can also configure session state by using the EnableSessionState value in the @ Page directive. The sessionState element enables you to specify the following options: The mode in which the session will store data.

Which mode disable the session state?

Off mode, which disables session state.


2 Answers

The session object may not be available this is dependent on when the error occured.

For example if an error occured on Begin_Request the session would not be available as it has not yet been created.

So in summary sometimes it will work sometimes not, depending on when the error occured.

Best to check the state of the session object before accesssing the session id e.g.

HttpContext context = HttpContext.Current;  if (context != null && context.Session != null) ... 
like image 148
swuk Avatar answered Oct 25 '22 16:10

swuk


check if any event missing in c# which is mapped to a control or issue in design part

like image 24
sankar raman Avatar answered Oct 25 '22 15:10

sankar raman