Session State Not Available In This Context - In Global.asax

你说的曾经没有我的故事 提交于 2019-12-12 08:25:02

问题


I am getting a 'session state not available in this context' error.

The error is nested in the sender parameter of a number of methods within the Global.asax file:

  • Application_BeginRequest
  • Application_AuthenticateRequest
  • Session_Start
  • Application_Error

The error happens on the very first page load (and all page loads thereafter).

I've added a completely new and empty page, WebForm1.aspx, to the project and made it my start page.

You'd think nothing could go wrong on an empty page. No code of mine is executed, as far as I can see, when loading an empty page. But I'm still getting the session state error.

My project works totally fine. No error pages are shown. There is no incorrect behavior at any time.

But the fact that this session error shows up in the sender parameter of my current project bugs me. This error is not generated in a blank, new project.

What can possibly be the cause of this error in my current project?

How can I trace where the error occurs? I can't set breakpoints in ASP.NET code that's under the hood. Or can I?

Here's my session configuration:

<pages enableSessionState="true" />
<sessionState mode="InProc" cookieless="false" timeout="20" />

Update:

I just found out that a brand new blank project also has this error in the sender object. It's visible in the Application_BeginRequest and Application_AuthenticateRequest methods. But the blank project isn't generating the Application_Error event, like my other project is.


回答1:


You are requesting for session state too early in the cycle. you need to do it in Application_AcquireRequestState




回答2:


"If a tree falls in a forest and no one is around to hear it, does it make a sound?"

Don't watch the Session property at the point where it is not available yet.



来源:https://stackoverflow.com/questions/13316252/session-state-not-available-in-this-context-in-global-asax

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