asp.net – ASP .NET检查会话

前端之家收集整理的这篇文章主要介绍了asp.net – ASP .NET检查会话前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
在我使用它之前,我想在基页类中检查Session是否存在,但是我发现如果它不存在,它只会通过检查引发异常:
if (Session != null)
{
    Session.Remove("foo");
}

将抛出此异常:

Session state can only be used when enableSessionState is set to true,either in a
configuration file or in the Page directive. Please also make sure that
System.Web.SessionStateModule or a custom session state module is included in the
\\ section in the application configuration.

检查发生在我的所有aspx页面派生的基页类的Load事件中.该应用程序启用了会话,并且它具有httpModules节点中列出的模块.这是一个经常使用会话的应用程序,通常没有问题.

我只在某些页面上收到此错误,而且大部分时间都不可靠.我知道我应该做一些不同的事情以防止错误,但我不确定是什么?

我可能在生命周期中过早地召集会议吗?
我没有正确检查会话是否可用?

解决方法

您可以使您的检查更安全 – Page.Session在Context.Session属性周围包装空检查.所以你应该能够检索Context.Session并检查它是否为null.如果是,那么会话状态不可用.

但它应该在Page_Load()中可用,因此指向其他地方的问题:)

原文链接:https://www.f2er.com/aspnet/248835.html

猜你在找的asp.Net相关文章