能够访问会话状态的Global

能够访问会话状态的Global

本文介绍了能够访问会话状态的Global.asax事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图访​​问会话状态在我的Global.asax为每个请求(网页,文档,PDF等)。我知道我不能这样做在的Application_BeginRequest,我以为我的Application_AcquireRequestState,但它不会工作,这很奇怪,因为它在另一个项目。

I'm trying to access the session state in my global.asax for every request (page, documents, PDF, etc.). I know i can't do that in Application_BeginRequest, and i thought i could in Application_AcquireRequestState, but it won't works, which is strange because it works in another project.

所以,我在找一个事件中,我总是有机会获得会话状态为每一个请求。

So, i'm looking for an event in which i would always have access to the session state for every request.

感谢

编辑:@Mike

我试着这样做

Sub Application_PreRequestHandlerExecute(ByVal sender As Object, ByVal e As EventArgs)
    Session("test") = "test"
End Sub

但我仍然得到错误,因为我没有访问会话状态。

But i still get errors as i don't have access to session state.

推荐答案

会话被Application_AcquireRequestState期间加载。您安全的赌注是建立 Application_ preRequestHandlerExecute 和访问它。

The session gets loaded during Application_AcquireRequestState. Your safe bet is to build Application_PreRequestHandlerExecute and access it there.

更新:并不是每一个请求有一个会话状态。您还需要检查空:如果(!System.Web.HttpContext.Current.Session = NULL)

Update: Not every request has a session state. You need to also check for null: if (System.Web.HttpContext.Current.Session != null).

这篇关于能够访问会话状态的Global.asax事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-31 10:20