通过 C# 访问时,<system.web> 为 null [重复]

问题描述 投票:0回答:1

我正在尝试从 web.config 中获取以下

timeout
值:

<!-- Majority of xml left out for brevity -->
<configuration>
    <configSections>...</configSections>
    <system.diagnostics>...</system.diagnostics>
    <system.web>
        <authentication mode="Forms">
          <forms timeout="1" /> <!-- Trying to get this value -->
        </authentication>
    </system.web>
</authentication>
</configuration>

我首先使用此 C# 代码。我的目标是首先访问

system.web
:

var value = ConfigurationManager.GetSection("configSections"); // null
var value1 = ConfigurationManager.GetSection("system.diagnostics"); // SystemDiagnosticsSection
var value2 = ConfigurationManager.GetSection("system.web"); // null

value
value2
都是
null
,但
value1
SystemDiagnosticsSection
的实例。

为什么我可以访问

system.diagnostics
但不能访问
system.web

c# asp.net xml web-config .net-4.7.2
1个回答
0
投票

感谢@Yong Shun的评论,我根据建议的答案解决了这个问题:

var authenticationSection = (AuthenticationSection)ConfigurationManager.GetSection("system.web/authentication");
var timeout = (int)authenticationSection.Forms.Timeout.TotalMinutes;
© www.soinside.com 2019 - 2024. All rights reserved.