簡體   English   中英

如何從 web.config 中的自定義部分讀取值

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM