zanhtet
zanhtet

Reputation: 2050

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.

Upvotes: 13

Views: 13594

Answers (2)

Chamira Lasantha
Chamira Lasantha

Reputation: 1

You can use this code

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

Upvotes: 0

VinayC
VinayC

Reputation: 49185

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");

Upvotes: 29

Related Questions