Session state can only be used when enableSessionState is set to true

asp.netsessionsession-stateweb.config

I am NOT using MVC, URL rewriting, custom HTTP modules, etc. On a basic call to the Session property off of either a web page inheriting System.Web.UI.Page or via System.Web.HttpContext.Current.Session, I get the following 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.

After doing some research, I have implemented Web.config as follows, but the error still occurs. I am debugging the web app out of Visual Studio 2010 using the built-in web server.

<configuration>
    <system.web>
        <compilation debug="true" targetFramework="4.0" />
        <sessionState mode="InProc" timeout="60" cookieless="false" />
        <pages enableSessionState="true" />
        <httpModules>
            <remove name="Session" />
            <add name="Session" type="System.Web.SessionState.SessionStateModule" />
        </httpModules>
    </system.web>
</configuration>

Best Answer

Your problem is straight forward. If you shift your code from a place that is executed after Session_Start global event, then you will see this problem go quite comfortably. As an example write your code in Page_Init event or override OnInit and do whatever you want to do with your Session there.

Hope this will work for you.

Related Topic