检查会话是否可用

时间:2020-12-04 16:55:20

I tried some code in Application_Error like this

我在Application_Error中尝试了一些像这样的代码

Session["mysession"] = "Some message";

but the problem is sometimes session is not available in Application_Error. So I want to check whether session is available or not.

但问题是有时会在Application_Error中无法使用会话。所以我想检查会话是否可用。

1 个解决方案

#1


16  

Session doesn't always exist within the context of the current Application_Error. Try the following:

Session并不总是存在于当前Application_Error的上下文中。请尝试以下方法:

protected void Application_Error(object sender, EventArgs e)
{
    if (Context.Handler is IRequiresSessionState || 
        Context.Handler is IReadOnlySessionState)
    {
         // Session exists
         Session["mysession"] = "Some message";
    }
}

#1


16  

Session doesn't always exist within the context of the current Application_Error. Try the following:

Session并不总是存在于当前Application_Error的上下文中。请尝试以下方法:

protected void Application_Error(object sender, EventArgs e)
{
    if (Context.Handler is IRequiresSessionState || 
        Context.Handler is IReadOnlySessionState)
    {
         // Session exists
         Session["mysession"] = "Some message";
    }
}