2009-08-26 67 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

這裏是你如何能夠確定是否啓用會話狀態:

相關問題