在调查 another problem 时我遇到了一个特殊的情况。
在 Global.asax 方法中,我有以下代码:
protected void Application_AcquireRequestState(object sender, EventArgs e)
{
if (!(Context.Handler is IRequiresSessionState || Context.Handler is IReadOnlySessionState)) // No session - no validation.
return;
DoSomething();
}
DoSomething()
方法调用抛出 NullReferenceException
它很简单,它唯一能做到的地方就是 HttpContext.Current.Session
为空。怎么会这样?
最佳答案
那时HttpContext.Current.Session
很可能是空的。事件在 session 为 的位置触发正在 aquired 无法保证此时它具有 已获得。您可能会 Hook 此事件的一个原因是您提供了一个自定义模块来实现您自己的 Session 对象。这样的自定义模块将在这里设置 session 。
如果你想使用 Session 对象,你应该使用 PostAquireRequestState
事件。
关于尽管有 IRequireSessionState/IReadOnlySessionState 处理程序,但 ASP.NET session 为 NULL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1588299/