【问题标题】:Accessing Session in MasterPage load event after a UrlRewrite在 UrlRewrite 之后访问 MasterPage 加载事件中的 Session
【发布时间】:2011-03-16 08:12:33
【问题描述】:

情况:

  • 在 Web.Config 中,我们使用 redirectMode="ResponseRewrite" 打开了 CustomErrors。
  • 在 MasterPage 的 Page_Load 中,我们访问 PageSession 属性。

问题:

当任何页面发生错误时,用户会被重定向(通过重写)到我们的 Error.aspx 页面。在 MasterPage 的 Page_Load 中,我们访问 Session 并获得一个 HttpException 告诉我们启用 SessionState。但是我们肯定启用了 SessionState。

问题:

我们如何在 MasterPage 的 Page_Load 事件中进行 UrlRewrite 后访问会话?

【问题讨论】:

  • 实际的异常是什么,或者确切的信息是什么?此外,每次引发导致 ResponseRewrite 的错误时都会发生这种情况,还是仅在某些错误时发生?
  • 每个未捕获的异常都会发生这种情况

标签: asp.net error-handling url-rewriting session-state


【解决方案1】:

试试这个...

Why is HttpContext.Session null when redirectMode = ResponseRewrite

摘录...

我还不知道这个问题的答案,但为了解决这个问题,我从我的 Web 配置中取出了 redirectMode 属性,并将自定义逻辑放入 Global.asax Application_Error 处理程序中以执行我想要的操作。我将异常替换为“用户友好”消息异常,但本质上传输逻辑是:

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

然后 Error.aspx 页面有 Page_Load 代码将错误拉出上下文并显示消息。

【讨论】:

    【解决方案2】:

    我们最终得到的解决方案:

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

    我们可以这样做,因为我们从 Session 获得的数据对于 ErrorPage 上的我们来说不是必需的。

    还要注意,访问HttpContext.Current.Session 不会抛出HttpException,而访问Page.Session 会。

    【讨论】:

    • 这个解决方案为我解决了一个类似的问题。谢谢。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多