开发者

To check if session is available or not

开发者 https://www.devze.com 2023-01-17 16:32 出处:网络
I tried some code in Application_Error like this Session[\"mysession\"] = \"Some message\"; but the problem is sometimes session is not available in Application_Error. So I want to check whether s开

I tried some code in Application_Error like this

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

but the problem is sometimes session is not available in Application_Error. So I want to check whether s开发者_开发知识库ession is available or not.


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

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

精彩评论

暂无评论...
验证码 换一张
取 消