【问题标题】:How could I read session state information in web.config我如何在 web.config 中读取会话状态信息
【发布时间】:2011-09-10 13:43:31
【问题描述】:

我在 web.config 中配置了会话状态。

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

现在,我想从代码隐藏中了解超时和 sqlConnectionString。请帮帮我。

【问题讨论】:

    标签: asp.net vb.net session web-config session-state


    【解决方案1】:

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

    但是,更好的方法是使用配置 API 来读取配置。在这种情况下,使用下面给出的代码获取对session state configuration 的引用,然后使用SqlConnectionString 和Timeout 等属性来查找必要的配置值。

    using System.Web.Configuration;
    
    ...
    
    var sessionSection = (SessionStateSection)WebConfigurationManager.GetSection("system.web/sessionState");
    

    【讨论】:

      【解决方案2】:

      你可以使用这个代码

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

      【讨论】:

        猜你喜欢
        • 2012-02-12
        • 1970-01-01
        • 2014-05-02
        • 2013-07-06
        • 1970-01-01
        • 2014-02-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-26
        相关资源
        最近更新 更多