2016-10-20 7 views
0

I持って、私はappleASP.NETのWeb.ConfigからカスタムのAppSettingsセクションを取得します

の色を取得するには、次のコードを使用しようとしています

<configSections> 
    <sectionGroup name="Fruits"> 
     <section name="Colors" /> 
    </sectionGroup> 
</configSections> 

<Fruits> 
    <add key="apple" value="red" /> 
    <add key="banana" value="yellow" /> 
</Fruits> 

次のカスタムアプリ-設定スタイルのconfigセクション

var settingsCollection = ConfigurationManager.GetSection("Fruits/Colors") as AppSettingsSection; 
    if (settingsCollection != null) 
    { 
     var color= settingsCollection.Settings["apple"]; 
    } 

settingsCollectionは、AppSettingsSectionにキャストできないため、オブジェクトが割り当てられていないため、上記のコードは機能しません。

私がウォッチウィンドウに次のように置くとき、私はKeyValueInternalCollection

ようなタイプを参照してください

AppSettingsSection

としてConfigurationManager.GetSection( "フルーツ/色")私は何をしないのですか?私は私のウェブアプリケーションの中にいるので、私はOpenExeConfigurationを使う必要はないと仮定し、いくつかのフォーラムで見つけたようにConfigurationManager.OpenWebConfigurationのような方法はありません。

答えて

0

このスニペットを試すことができます。

NameValueCollection Fruits = (NameValueCollection)ConfigurationManager.GetSection("Fruits"); 

foreach (string key in Fruits.Keys) 
{ 
    string[] values = Fruits.GetValues(key); 
    foreach (string value in values) 
    { 
     Label1.Text += key + " - " + value + "<br>"; 
    } 
} 
関連する問題