What is the best way to do a boolean test in C# to determine if ASP.NET sessions are enabled? I don't want to use a try-catch block and Sessions != null throws an Exception.
Regards.
What is the best way to do a boolean test in C# to determine if ASP.NET sessions are enabled? I don't want to use a try-catch block and Sessions != null throws an Exception.
Regards.
You want to interrogate the EnableSessionState property in Web.config
.
You will not get an exception if you use HttpContext.Current
:
if(HttpContext.Current.Session != null)
{
// Session!
}
You can use something like this(Pseudo code)
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
}