Accessing Session in MasterPage load event after a UrlRewrite
Situation:
- In Web.Config we have CustomErrors turned on with
redirectMode="ResponseRewrite"
. - In
Page_Load
of our MasterPage we a开发者_运维技巧ccess theSession
property of thePage
.
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?
Try this instead...
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.
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.
精彩评论