[英]How to read values from custom section in web.config
I have added a custom section called secureAppSettings
to my web.config file:我在 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
is decrypted and has two keys inside it. secureAppSettings
被解密并且里面有两个密钥。
Now in my code, I tried to access the keys like this:现在在我的代码中,我尝试访问这样的键:
string userName = System.Configuration.ConfigurationManager.secureAppSettings["userName"];
string userPassword = System.Configuration.ConfigurationManager.secureAppSettings["userPassword"];
But null
is returning for these fields.但是
null
正在返回这些字段。
How can I get the field values?如何获取字段值?
You could access them as key/value pairs:您可以将它们作为键/值对访问:
NameValueCollection section = (NameValueCollection)ConfigurationManager.GetSection("secureAppSettings");
string userName = section["userName"];
string userPassword = section["userPassword"];
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.