[英]How to read values from custom section in web.config
我在 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"];
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.