简体   繁体   中英

How could I read session state information in web.config

I configured session state in web.config.

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

Now, I want to know timeout and sqlConnectionString from code-behind. Please, help me.

You can use Session.Timeout to know timeout value.

However, better way is to use configuration API to read configuration. In this case, use code given below to get reference to session state configuration and then use properties such as SqlConnectionString and Timeout to find the necessary configured values.

using System.Web.Configuration;

...

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

You can use this code

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM