如何使用会话变量?

时间:2013-08-16 07:07:28

标签: c# asp.net .net .net-4.5 asp.net-4.5

经过Google的大量研究,我找不到解决问题的方法。

当我想将值保存到Session变量时,一切正常:

Session["idResult"] = youthID;

但是当我想用以下代码行获取值时:

youthID = (int)Session["idResult"];

我收到错误:只有在配置文件或Page指令中将enableSessionState设置为true时才能使用会话状态。还请确保System.Web.SessionStateModule或自定义会话状态模块包含在应用程序配置的<configuration>\<system.web>\<httpModules>部分中。

我已尝试将其添加到web.config:EnableSessionState="True"

还有:

<httpModules><add name="Session" type="System.Web.SessionState.SessionStateModule"/>     </httpModules>

以及许多其他可能性,但它不起作用

我的默认web.config是:

    <?xml version="1.0"?>
<!--
  For more information on how to configure your ASP.NET application, please visit
  http://go.microsoft.com/fwlink/?LinkId=169433
  -->
<configuration>
    <appSettings>
    <add key="ValidationSettings:UnobtrusiveValidationMode" value="None"/>
  </appSettings>
  <system.web>
    <compilation debug="true" targetFramework="4.5"/>
    <httpRuntime targetFramework="4.5"/>
  </system.web>
</configuration>

1 个答案:

答案 0 :(得分:0)

请在配置

中的部分启用会话状态
 <system.web>
      <pages enableSessionState="true"> 
 </system.web>

或者您可以将其添加到页面

 <%@Page enableSessionState="true">