帮助! ASP.NET C#项目无法访问线程或定时事件中的会话变量. [英] Help! ASP.NET C# Project Unable to access Session Variable in Thread or Timed Event.
问题描述
我在ASP.NET C#中有一个网页,该网页使用
创建会话变量
I have a webpage in ASP.NET C# that creates a session variable using
Session["State"]="State1";
我尝试在另一个名为WaitPage的页面中访问此会话变量,并且它有效.
当我尝试在WaitPage中的线程中访问此Session变量或尝试在定时事件中访问Session变量时,出现错误.
只有在配置文件或Page指令中将enableSessionState
设置为true时,才可以使用会话状态.还请确保在应用程序配置的<configuration>\<system.web>\<httpModules>
部分中包含System.Web.SessionStateModule
或自定义会话状态模块.
我已经在两个网页中都启用了"EnableSessionState=true"
.
我在web.config的httpModules部分中具有必需的add name="Session" type="System.Web.SessionState.SessionStateModule"
.
为什么会出现此错误?你能帮我解决这个问题吗?
谢谢
Jobin Thomas
I try to access this session variable in another page called WaitPage and it works.
When i try to access this Session variable in a thread in WaitPage or try to access the session variable in a timed event, I get the error.
Session state can only be used when enableSessionState
is set to true, either in a configuration file or in the Page directive. Please also make sure that System.Web.SessionStateModule
or a custom session state module is included in the <configuration>\<system.web>\<httpModules>
section in the application configuration.
I have already enabled "EnableSessionState=true"
in both webpages.
I have the required add name="Session" type="System.Web.SessionState.SessionStateModule"
in httpModules section in web.config.
Why am I getting this error? Can you help me fix this?
Thanks
Jobin Thomas
推荐答案
您不能直接访问Thread
中的Session
变量.
Session
与HttpContext.Current
对象相关联.在处理Http请求时,将实例化HttpContext.Current
,从而实例化HttpContext.Current.Session
.当进行http请求并呈现页面时,这些对象将超出范围.
在页面事件上启动的线程中,异步运行并且HttpContext.Current.Session
为null.因此您无法访问Session对象.
一种方式:
将创建一个ashx or aspx
页来执行会话操作,并允许Thread
通过WebRequest
调用aspx/ashx页.
You cannot directly access theSession
variable in aThread
.
Session
is associated withHttpContext.Current
object. When a Http request is under process aHttpContext.Current
is instantiated and thus theHttpContext.Current.Session
is instantiated. These objects gets out of scope when a http request is proceesed and page is rendered.
In Threads that are started on a page event runs async and there theHttpContext.Current.Session
is null. so you cant access the Session object.
One way :
Aashx or aspx
page would be created to do the session manipulation and let theThread
calls that aspx/ashx page through aWebRequest
.
错误本身是不言自明的.
只有在配置文件或Page指令中将enableSessionState设置为true时,才可以使用会话状态.
这只是意味着,
您需要包括< pages ...> web.config中的属性:
Error in itself is quite self-explanatory.
Session state can only be used when enableSessionState is set to true, either in a configuration file or in the Page directive...
This simply means,
You need to include a <pages... > attribute in web.config:
<pages enableSessionState = "true" />
或
只需在<%@页面...%>中启用SessionState即可. ASPX页面中的指令,例如:
OR
Just Enable SessionState in the <%@ Page ...%> directive in your ASPX page, like:
<%@ Page enableSessionState = "true" %>
您可能需要启用会话存储.
请参考
http://support.microsoft.com/kb/317604 [
You may need to enable session store.
Refer this
http://support.microsoft.com/kb/317604[^]
这篇关于帮助! ASP.NET C#项目无法访问线程或定时事件中的会话变量.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!