Session state is not available in this context

response is not available in this context global asax mvc
request is not available in this context asp net c#
system web sessionstate httpsessionstate this string get returned null
httpcontext current session threw an exception of type 'system nullreferenceexception
namespace for session in c#
application_prerequesthandlerexecute session null

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

}

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) ...

[Solved] Session state is not available in this context., Use HttpContext.Current.Session instead of just Session. That should give the app handler the correct context. It is worth noting that checking Context then Context.Session for being null avoid the exception. While accessing Session directly from application object raises the exception if not available, instead of yielding null .

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

How to fix Session state is not available in this context IIS Error (Any , Server Error in '/RA' Application. Session state is not available in this context. Description: An unhandled exception occurred during the  The HttpApplication 's implementation of the Session property throws an HttpException with the message Session state is not available in this context. rather than returning a null reference. Some examples of HttpHandler that do not implement session are the default handlers for normally static resources, such as image and CSS files.

Application_error can fire in situations where a session is not present, for example when the garbage collector cleans up. The source of the error may not have been a user thread.

Just check whether session is null first.

Simon

Sessionstate is not available in this context global.asax, we execute the application Sessionstate is not available in this context error in global.asax file. plz guide me how to solve the problem. The error  Thanks for contributing an answer to Stack Overflow! Please be sure to answer the question.Provide details and share your research! But avoid …. Asking for help, clarification, or responding to other answers.

You may get this error if you are using an Out-of-Process state mode for ASP.NET state server. You need to mark any classes you wish to save to session state with the [Serializable] attribute

"Session state is not available in this context" exception accessing , "Session state is not available in this context" exception accessing ASP.NET Session in PreRequestHandlerExecute event. March 31, 2008 by Ben Day. Don't store sensitive data in session state. The user might not close the browser and clear the session cookie. Some browsers maintain valid session cookies across browser windows. A session might not be restricted to a single user—the next user might continue to browse the app with the same session cookie.

Session state is not available in this context., "Session state is not available in this context." code of global.asax file: void Application_Error(object sender, EventArgs e) { // Code that runs  Try HttpContext.Current.Session["BranchId"]. I did not check but you might find Session is not available at this point in the page life cycle. Regardless, I would rework the code to remove the dependency on Session. You could place the BranchId inside the auth token or simple query the DB to get the ID.

[RESOLVED] Error [Session state is not available in this context] in , [RESOLVED] Error [Session state is not available in this context] in debug mode. The Session property provides programmatic access to the properties and methods of the HttpSessionState class. In order to use session state you have to enable it. For information about how to enable session state, see Configuring Session State in ASP.NET Session State Overview. For information about how to save values in session state, see How

Session state is not available in the current context, axd files containing the ajax javascript and you see a beautiful ASP.Net error page displaying "Session state is not available in this context.". Huh  If, during transmission, the session were to be hijacked, the user's credentials would not be compromised. ASP.NET provides a session state, which is available as the HttpSessionState class, as a method of storing session-specific information that is visible only within the session.

Comments
  • did you have an answer for this?
  • siderite.blogspot.com/2009/01/… this one saved my life.
  • Also HttpContext.Current might be null so the if clause should read: if (context != null && context.Session != null)
  • It is worth noting that checking Context then Context.Session for being null avoid the exception. While accessing Session directly from application object raises the exception if not available, instead of yielding null. See stackoverflow.com/a/6021261/1178314