如何从web.config中的自定义节读取值
本文关键字:自定义 读取 web config | 更新日期: 2023-09-27 17:59:04
我在web.config文件中添加了一个名为secureAppSettings
的自定义部分:
<configuration>
<configSections>
<section name="secureAppSettings" type="System.Configuration.NameValueSectionHandler, System, Version=2.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>
<secureAppSettings>
<add key="userName" value="username"/>
<add key="userPassword" value="password"/>
</secureAppSettings>
</configuration>
secureAppSettings
被解密,并且里面有两个密钥
现在在我的代码中,我试图访问这样的密钥:
string userName = System.Configuration.ConfigurationManager.secureAppSettings["userName"];
string userPassword = System.Configuration.ConfigurationManager.secureAppSettings["userPassword"];
但null
正在为这些字段返回。
如何获取字段值?
您可以将它们作为键/值对进行访问:
NameValueCollection section = (NameValueCollection)ConfigurationManager.GetSection("secureAppSettings");
string userName = section["userName"];
string userPassword = section["userPassword"];