Determine if ASP.NET sessions are enabled

What is the best way to perform a logical test in C # to determine if ASP.NET sessions are enabled? I do not want to use a try-catch block and Session! = Null throws an exception.

Sincerely.

+3
source share
4 answers

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
}
+1
source

You will not get an exception if you use HttpContext.Current:

if(HttpContext.Current.Session != null)
{
    // Session!
}
+3
source

You want to question property EnableSessionState in Web.config.

+2
source

Here's how you can determine if session state is enabled:

PagesSection pagesSection = ConfigurationManager.GetSection("system.web/pages") as PagesSection;

if ((null != pagesSection) && (pagesSection.EnableSessionState == PagesEnableSessionState.True))
    // Session state is enabled
else
    // Session state is disabled (or readonly)
+1
source

Source: https://habr.com/ru/post/1716138/


All Articles