Why is HttpContext.Session null when redirectMode = ResponseRewrite

眉间皱痕 提交于 2019-11-28 07:18:41

问题


As is suggested elsewhere, I am using redirectMode = ResponseRewrite in my custom error configuration so my custom error page can access the exception info. This has worked like a charm for some time.

In adding some more "help the user recover from an error" type functionality, we need a piece of info that has been previously stored in Session. When implementing this, I found that the various avenues to Session end in null when redirectMode=ResponseRewrite, but they are all populated when redirectMode=ResponseRedirect (or isn't defined).

Anyone know why? It seems odd that we'd have to choose between having exception info (ResponseRewrite) or having Session (ResponseRedirect).

The MSDN article on Rich Custom Error handling tells me that Session is only available when the control passing method is Server.Transfer, which is what I assumed ResponseRewrite used under the hood. Evidently that isn't the case.


回答1:


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.



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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!