'Session' threw an exception of type system.web.httpexception
I'm trying to get a Session["user"]
on Page_Load
but it keep giving me this crash:
'Session' threw an exception of type system.web.httpexception
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 \\ section in the application configuration.
Here is my web.config
<configuration>
<system.web>
<pages enableSessionState="true" />
<httpModules>
<add name="Session" type="System.Web.SessionState.SessionStateModule" />
</httpModules>
</system.web>
</configuration>
There are other things inside the configuration tag, but the important part is this one, where the config is correct, but the error still the same.
Why is this happening?
No big deal in the .aspx
protected void Page_Load(object sender, EventArgs e)
{
try
{
if (Session["user"] == null)
Response.Redirect("~/Login.aspx");
}
}
Solution 1:
I just had a similar problem and found a solution at That Hyphenated Website of all places. My code:
void Application_Error(object sender, EventArgs e )
{
Exception ex = Server.GetLastError();
if ( ex != null )
{
if ( ex.GetBaseException() != null )
{
ex = ex.GetBaseException();
}
if ( Session != null ) // Exception occurred here.
{
Session[ "LastException" ] = ex;
}
Toolbox.Log( LogLevel.Error, "An Application Error Occurred", ex );
}
}
... and by changing the indicated line to:
if ( HttpContext.Current.Session != null )
the code now performs as I would expect. Caveat Emptor: my link to that hyphenated website behaves differently on this page than it does from the page of Google results to my query
'session' threw an exception of type 'system.web.httpexception'