Kei*_*ler 4 c# asp.net session
在 C# 中进行布尔测试以确定是否启用 ASP.NET 会话的最佳方法是什么?我不想使用 try-catch 块并且 Sessions != null 抛出异常。
问候。
如果您使用,则不会出现异常HttpContext.Current:
if(HttpContext.Current.Session != null)
{
// Session!
}
Run Code Online (Sandbox Code Playgroud)
你可以使用这样的东西(伪代码)
XmlDocument document = new XmlDocument();
document.Load("Web.Config");
XmlNode pagesenableSessionState = document.SelectSingleNode("//Settings[@Name = 'pages']/Setting[@key='enableSessionState']");
if(pagesenableSessionState .Attributes["value"].Value =="true)
{
//Sessions are enabled
}
else
{
//Sessions are not enabled
}
Run Code Online (Sandbox Code Playgroud)