I reproduced the problem, but I can't understand why this happens. In the Application_Error
handler I can access the Session variable, but when the page loads it becomes null
.
I found a workaround here that solves the problem. You need to remove the redirectMode
from your web.config and manually do the Server.Transfer
when you get an error. So this is the web.config:
<customErrors mode="On" defaultRedirect="~/Error.aspx"/>
And add this to the Global.asax.cs
file:
void Application_Error(object sender, EventArgs e)
{
if(Context.IsCustomErrorEnabled)
{
Server.Transfer("~/Error.aspx");
}
}
To specify different error pages depending on the error, you can access the error code like this:
HttpException httpException = (HttpException) Server.GetLastError();
int httpCode = httpException.GetHttpCode();
switch (httpCode)
{
case 500: Server.Transfer("~/Pages/Error.aspx");break;
case 404: Server.Transfer("~/Pages/PageNotFound.aspx");break;
default: Server.Transfer("~/Pages/Error.aspx");break;
}