views:

94

answers:

2

Situation:

  • In Web.Config we have CustomErrors turned on with redirectMode="ResponseRewrite".
  • In Page_Load of our MasterPage we access the Session property of the Page.

Problem:

When an Error occurs on any page the user gets redirected (via Rewrite) to our Error.aspx Page. There in the Page_Load of the MasterPage we access the Session and get an HttpException telling us to enable SessionState. But we have the SessionState enabled, definitly.

Question:

How can we access the session after a UrlRewrite in the Page_Load Event of our MasterPage?

+1  A: 

Try this instead...

http://stackoverflow.com/questions/1589566/why-is-httpcontext-session-null-when-redirectmode-responserewrite

excerpt...

I don't know the answer to the question yet, but to get past it, I took the redirectMode attribute out of my web config and put custom logic in the Global.asax Application_Error handler to do what I wanted. I am replacing the exception with a "user friendly" message exception, but essentially the transfer logic is:

if(Context.IsCustomErrorEnabled)
{
Server.Transfer("~/Error.aspx");
}

Then the Error.aspx page has Page_Load code to pull the error out of context and display the message.

Carter
A: 

The solution we ended up with:

if( HttpContext.Current.Session != null )
{
    //Access Session here...
}

We can do this because the data we get from the Session is not Essential for us on the ErrorPage.

Also note that accessing HttpContext.Current.Session will not throw an HttpException whereas accessing Page.Session would.

Thomas Schreiner