1

web.config の個人セクションを読むには?

<MyPersonalSection>
    <add name="toto" enable="true" URL="http://localhost:43242" />
    <add name="titi" enable="false" URL="http://localhost:98762" />
<MyPersonalSection/>

name 値で enable 値および/または URL 値を取得したいと思います。

私もこの間違いをしています: 認識されない構成セクション MyPersonalSection

私はしようとしている

var config = ConfigurationManager.GetSection("MyPersonalSection");

4

3 に答える 3

3

これはそのためのクールな例です。

于 2012-07-26T12:37:14.280 に答える
0

必要なものを取得するために、カスタム構成ハンドラーを作成する必要はありません。単にキーと値のエントリが必要な場合に使用できる組み込みの構成ハンドラーがあります。ただし、key代わりにnameandvalueの代わりに使用する必要がありますURL。例えば:

<configuration>
 <configSections>
  <section name="MyPersonalSection" type="System.Configuration.NameValueSectionHandler" />
 </configSections>
<MyPersonalSection>
    <add key="toto" value="http://localhost:43242" />
    <add key="titi" value="http://localhost:98762" />
</MyPersonalSection>
</configuration>

また、コードを介してそれらにアクセスできます。

var myValues = ConfigurationSettings.GetConfig("MyPersonalSection") as NameValueCollection;
var url = myValues["toto"];

「totoUrl」や「titiUrl」のように、値がどうあるべきかを明確にする方法でキーに名前を付けることをお勧めします。

文字列と値のペア以外のものが必要な場合は、独自のカスタム ハンドラーを作成する必要があります。

于 2012-07-26T13:00:08.217 に答える