2009-08-26 101 views

回答

1

您可以使用这样的(伪代码)

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 
} 
+0

由于会话状态也可以通过页面指令和其他配置文件(machine.config或更高目录级别的web.config)启用,因此此答案至少不完整,最坏的情况是错误的。 – wensveen 2015-09-28 13:47:48

2

你想询问EnableSessionState财产Web.config

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

如果使用HttpContext.Current你不会得到一个异常:

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

这将引发和异常。 – 2009-08-27 05:59:18

1

这里是你如何能够确定是否启用会话状态: