Asp.net

我如何在 web.config 中讀取會話狀態資訊

  • December 6, 2016

我在 web.config 中配置了會話狀態。

<sessionState cookieless="AutoDetect" timeout="5" sqlConnectionString="....."/>

現在,我想從程式碼隱藏中了解超時和 sqlConnectionString。請幫我。

您可以使用Session.Timeout來了解超時值。

但是,更好的方法是使用配置 API 來讀取配置。在這種情況下,使用下面給出的程式碼來獲取對會話狀態配置的引用,然後使用SqlConnectionStringTimeout等屬性來查找必要的配置值。

using System.Web.Configuration;

...

var sessionSection = (SessionStateSection)WebConfigurationManager.GetSection("system.web/sessionState");

您可以使用此程式碼

Configuration config = WebConfigurationManager.OpenWebConfiguration("~/Web.config");
SessionStateSection section = (SessionStateSection)config.GetSection("system.web/sessionState");

引用自:https://stackoverflow.com/questions/6274079