我正在尝试从 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
?
感谢@Yong Shun的评论,我根据建议的答案解决了这个问题:
var authenticationSection = (AuthenticationSection)ConfigurationManager.GetSection("system.web/authentication");
var timeout = (int)authenticationSection.Forms.Timeout.TotalMinutes;