WebConfigurationManager.GetSection メソッド
定義
重要
一部の情報は、リリース前に大きく変更される可能性があるプレリリースされた製品に関するものです。 Microsoft は、ここに記載されている情報について、明示または黙示を問わず、一切保証しません。
現在の Web アプリケーションの既定の構成ファイルから、指定した構成セクションを取得します。
オーバーロード
| 名前 | 説明 |
|---|---|
| GetSection(String) |
現在の Web アプリケーションの構成ファイルから、指定した構成セクションを取得します。 |
| GetSection(String, String) |
指定した場所にある Web アプリケーションの構成ファイルから、指定した構成セクションを取得します。 |
GetSection(String)
現在の Web アプリケーションの構成ファイルから、指定した構成セクションを取得します。
public:
static System::Object ^ GetSection(System::String ^ sectionName);
public static object GetSection(string sectionName);
static member GetSection : string -> obj
Public Shared Function GetSection (sectionName As String) As Object
パラメーター
- sectionName
- String
構成セクション名。
返品
指定した構成セクション オブジェクト。セクションが存在しない場合は null 。 ランタイム操作として GetSection(String) を使用する場合は、セキュリティ制限が存在することを忘れないでください。 たとえば、変更のために実行時にセクションにアクセスできない場合があります。
例外
有効な構成ファイルを読み込めませんでした。
例
このセクションの例では、 GetSection メソッドを使用して構成情報にアクセスする方法を示します。
次の例は、Web アプリケーションまたはコンソール アプリケーションからアクセスできるセクションを示しています。
Note
この例では、 GetWebApplicationSection メソッドを使用して、構成ファイルから ConfigurationSection オブジェクトを取得する方法を示します。
// Show how to use the GetSection(string).
// to access the connectionStrings section.
static void GetConnectionStringsSection()
{
// Get the connectionStrings section.
ConnectionStringsSection connectionStringsSection =
WebConfigurationManager.GetSection("connectionStrings")
as ConnectionStringsSection;
// Get the connectionStrings key,value pairs collection.
ConnectionStringSettingsCollection connectionStrings =
connectionStringsSection.ConnectionStrings;
// Get the collection enumerator.
IEnumerator connectionStringsEnum =
connectionStrings.GetEnumerator();
// Loop through the collection and
// display the connectionStrings key, value pairs.
int i = 0;
Console.WriteLine("[Display the connectionStrings]");
while (connectionStringsEnum.MoveNext())
{
string name = connectionStrings[i].Name;
Console.WriteLine("Name: {0} Value: {1}",
name, connectionStrings[name]);
i += 1;
}
Console.WriteLine();
}
' Show how to use the GetSection(string).
' to access the connectionStrings section.
Shared Sub GetConnectionStringsSection()
' Get the connectionStrings section.
Dim connectionStringsSection As ConnectionStringsSection = _
WebConfigurationManager.GetSection("connectionStrings")
' Get the connectionStrings key,value pairs collection.
Dim connectionStrings As ConnectionStringSettingsCollection = _
connectionStringsSection.ConnectionStrings
' Get the collection enumerator.
Dim connectionStringsEnum As IEnumerator = _
connectionStrings.GetEnumerator()
' Loop through the collection and
' display the connectionStrings key, value pairs.
Dim i As Integer = 0
Console.WriteLine("[Display the connectionStrings]")
While connectionStringsEnum.MoveNext()
Dim name As String = connectionStrings(i).Name
Console.WriteLine("Name: {0} Value: {1}", _
name, connectionStrings(name))
i += 1
End While
Console.WriteLine()
End Sub
注釈
GetSectionが Web アプリケーション内から呼び出されると、Web アプリケーション構成階層に従って、システムによって選択された構成ファイルからセクションが取得されます。
Caution
アプリケーションで HTTP とは異なるプロトコルを使用する場合、セクション名とそのパラメーター リスト内のパスの両方を受け取る GetSection オーバーロードが使用されます。 構成階層レベルに関する前提条件をシステムが行うことができないため、構成ファイルのパスを指定する必要があります。 セクション名のみを受け取る GetSection オーバーロードを使用する場合、システムは常にアプリケーション レベルで構成設定を返そうとします。 ただし、指定したパスが現在のアプリケーションの外部にある場合、パスを受け取るオーバーロードは、現在実行中のアプリケーションのアプリケーション レベルの構成設定も返されることに注意してください。
クライアント アプリケーション内から GetSection を呼び出す場合があります。 この場合、クライアント構成階層に従って、システムによって選択された構成ファイルから既定のセクションが取得されます。 通常、マップされた構成がない限り、これは Machine.config ファイルです。 マッピング構成ファイルについては、次に説明するマッピング方法を参照してください。
Note
GetSection メソッドは、アプリケーションが実行される階層レベルで構成ファイルのセクションを操作するランタイム メソッドです。 実行時以外の操作の場合は、代わりに GetSection を使用します。 このメソッドは、構成ファイルを開くオーバーロードされたメソッドの 1 つを使用して取得する構成ファイルの指定されたセクションで動作 OpenWebConfiguration。
注意 (継承者)
戻り値は、使用する前に予想される構成の種類にキャストする必要があります。 例外のキャストを回避するには、C# の as 演算子のような条件付きキャスト操作を使用する必要があります。
こちらもご覧ください
適用対象
GetSection(String, String)
指定した場所にある Web アプリケーションの構成ファイルから、指定した構成セクションを取得します。
public:
static System::Object ^ GetSection(System::String ^ sectionName, System::String ^ path);
public static object GetSection(string sectionName, string path);
static member GetSection : string * string -> obj
Public Shared Function GetSection (sectionName As String, path As String) As Object
パラメーター
- sectionName
- String
構成セクション名。
- path
- String
仮想構成ファイルのパス。
返品
指定した構成セクション オブジェクト。セクションが存在しない場合は null 。 実行時操作として GetSection(String, String) を使用する場合は、セキュリティ制限が存在することを忘れないでください。 たとえば、変更のために実行時にセクションにアクセスできない場合があります。
例外
このメソッドは、Web アプリケーションの外部から呼び出されます。
有効な構成ファイルを読み込めませんでした。
例
次の例は、 GetSection メソッドを使用して構成情報にアクセスする方法を示しています。
Note
この例では、 GetSection メソッドを使用して、指定した構成ファイルから ConfigurationSection オブジェクトを取得する方法を示します。
// Show the use of GetSection(string, string).
// to access the connectionStrings section.
static void GetSection2()
{
try
{
// Get the connectionStrings section for the
// specified Web app. This GetSection overload
// can olny be called from within a Web application.
ConnectionStringsSection connectionStringsSection =
WebConfigurationManager.GetSection("connectionStrings",
"/configTest") as ConnectionStringsSection;
// Get the connectionStrings key,value pairs collection
ConnectionStringSettingsCollection connectionStrings =
connectionStringsSection.ConnectionStrings;
// Get the collection enumerator.
IEnumerator connectionStringsEnum =
connectionStrings.GetEnumerator();
// Loop through the collection and
// display the connectionStrings key, value pairs.
int i = 0;
Console.WriteLine("[Display connectionStrings]");
while (connectionStringsEnum.MoveNext())
{
string name = connectionStrings[i].Name;
Console.WriteLine("Name: {0} Value: {1}",
name, connectionStrings[name]);
i += 1;
}
Console.WriteLine();
}
catch (InvalidOperationException e)
{
string errorMsg = e.ToString();
Console.WriteLine(errorMsg);
}
}
' Show the use of GetSection(string, string).
' to access the connectionStrings section.
Shared Sub GetSection2()
Try
' Get the connectionStrings section for the
' specified Web app. This GetSection overload
' can olny be called from within a Web application.
Dim connectionStringsSection As ConnectionStringsSection = _
WebConfigurationManager.GetSection( _
"connectionStrings", "/configTest")
' Get the connectionStrings key,value pairs collection
Dim connectionStrings As ConnectionStringSettingsCollection = _
connectionStringsSection.ConnectionStrings
' Get the collection enumerator.
Dim connectionStringsEnum As IEnumerator = _
connectionStrings.GetEnumerator()
' Loop through the collection and
' display the connectionStrings key, value pairs.
Dim i As Integer = 0
Console.WriteLine("[Display connectionStrings]")
While connectionStringsEnum.MoveNext()
Dim name As String = connectionStrings(i).Name
Console.WriteLine("Name: {0} Value: {1}", _
name, connectionStrings(name))
i += 1
End While
Console.WriteLine()
Catch e As InvalidOperationException
Dim errorMsg As String = e.ToString()
Console.WriteLine(errorMsg)
End Try
End Sub
注釈
GetSectionが Web アプリケーション内から呼び出されると、構成階層内の指定されたパスによって定義された構成ファイルからセクションが取得されます。
Caution
アプリケーションで HTTP とは異なるプロトコルを使用する場合、セクション名とそのパラメーター リスト内のパスの両方を受け取る GetSection オーバーロードが使用されます。 構成階層レベルに関する前提条件をシステムが行うことができないため、構成ファイルのパスを指定する必要があります。 セクション名のみを受け取る GetSection オーバーロードを使用する場合、システムは常にアプリケーション レベルで構成設定を返そうとします。 ただし、指定したパスが現在のアプリケーションの外部にある場合、パスを受け取るオーバーロードは、現在実行中のアプリケーションのアプリケーション レベルの構成設定も返されることに注意してください。
このメソッドは、クライアント アプリケーション内から呼び出すことはできません。
現在の Web アプリケーション ディレクトリ レベルにある構成ファイルから構成セクションを取得する場合は、 GetSection メソッドを使用します。
Note
GetSection メソッドは、アプリケーションが実行される階層レベルの構成ファイルのセクションで動作するランタイム メソッドです。 実行時以外の操作の場合は、代わりに GetSection を使用します。 このメソッドは、 open 構成ファイルのいずれかの方法を使用して取得する構成ファイルの指定されたセクションで動作します。
注意 (継承者)
戻り値は、使用する前に予想される構成の種類にキャストする必要があります。 例外のキャストを回避するには、C# の as 演算子のような条件付きキャスト操作を使用する必要があります。