WebConfigurationManager.GetSection 메서드
정의
중요
일부 정보는 릴리스되기 전에 상당 부분 수정될 수 있는 시험판 제품과 관련이 있습니다. Microsoft는 여기에 제공된 정보에 대해 어떠한 명시적이거나 묵시적인 보증도 하지 않습니다.
현재 웹 애플리케이션의 기본 구성 파일에서 지정된 구성 섹션을 검색합니다.
오버로드
| Name | Description |
|---|---|
| GetSection(String) |
현재 웹 애플리케이션의 구성 파일에서 지정된 구성 섹션을 검색합니다. |
| GetSection(String, String) |
지정된 위치에 있는 웹 애플리케이션의 구성 파일에서 지정된 구성 섹션을 검색합니다. |
GetSection(String)
현재 웹 애플리케이션의 구성 파일에서 지정된 구성 섹션을 검색합니다.
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 .
다음 예제에서는 웹 애플리케이션 또는 콘솔 애플리케이션에서 액세스할 수 있는 섹션을 보여줍니다.
메모
이 예제에서는 메서드를 사용하여 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 웹 애플리케이션 구성 계층 구조에 따라 시스템에서 선택한 구성 파일에서 섹션을 가져옵니다.
Caution
애플리케이션이 HTTP와 다른 프로토콜을 사용하는 경우 해당 매개 변수 목록의 GetSection 경로와 섹션 이름을 모두 사용하는 오버로드를 사용할 수 있습니다. 시스템에서 구성 계층 수준에 대해 어떤 가정도 할 수 없으므로 구성 파일 경로를 지정해야 합니다. 섹션 이름만 사용하는 오버로드를 사용하는 GetSection 경우 시스템은 항상 애플리케이션 수준에서 구성 설정을 반환하려고 시도합니다. 그러나 지정된 경로가 현재 애플리케이션 외부에 있는 경우 경로를 사용하는 오버로드는 현재 실행 중인 애플리케이션에 대한 애플리케이션 수준 구성 설정도 반환합니다.
클라이언트 애플리케이션 내에서 호출 GetSection 할 수 있습니다. 이 경우 클라이언트 구성 계층 구조에 따라 시스템에서 선택한 구성 파일에서 기본 섹션을 가져옵니다. 일반적으로 매핑된 구성이 없으면 이 파일은 Machine.config 파일입니다. 매핑 구성 파일의 경우 다음에 설명된 매핑 방법을 참조하세요.
메모
이 GetSection 메서드는 애플리케이션이 실행되는 계층 구조 수준에서 구성 파일의 섹션에서 작동하는 런타임 메서드입니다. 런타임이 아닌 작업의 경우 대신 사용합니다 GetSection . 이 메서드는 구성 파일을 열기 위해 오버로드된 메서드 중 하나를 사용하여 가져오는 구성 파일 OpenWebConfiguration의 지정된 섹션에서 작동합니다.
상속자 참고
반환 값은 사용하기 전에 예상된 구성 형식으로 캐스팅해야 합니다. 가능한 캐스팅 예외를 방지하려면 C#의 연산자처럼 as 조건부 캐스팅 작업을 사용해야 합니다.
추가 정보
적용 대상
GetSection(String, String)
지정된 위치에 있는 웹 애플리케이션의 구성 파일에서 지정된 구성 섹션을 검색합니다.
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) 때는 보안 제한이 존재합니다. 예를 들어 수정을 위해 런타임에 섹션에 액세스하지 못할 수 있습니다.
예외
이 메서드는 웹 애플리케이션 외부에서 호출됩니다.
유효한 구성 파일을 로드할 수 없습니다.
예제
다음 예제에서는 메서드를 사용하여 구성 정보에 액세스하는 GetSection 방법을 보여줍니다.
메모
이 예제에서는 메서드를 사용하여 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 구성 계층의 지정된 경로에 정의된 구성 파일에서 섹션을 가져옵니다.
Caution
애플리케이션이 HTTP와 다른 프로토콜을 사용하는 경우 해당 매개 변수 목록의 GetSection 경로와 섹션 이름을 모두 사용하는 오버로드를 사용할 수 있습니다. 시스템에서 구성 계층 수준에 대해 어떤 가정도 할 수 없으므로 구성 파일 경로를 지정해야 합니다. 섹션 이름만 사용하는 오버로드를 사용하는 GetSection 경우 시스템은 항상 애플리케이션 수준에서 구성 설정을 반환하려고 시도합니다. 그러나 지정된 경로가 현재 애플리케이션 외부에 있는 경우 경로를 사용하는 오버로드는 현재 실행 중인 애플리케이션에 대한 애플리케이션 수준 구성 설정도 반환합니다.
이 메서드는 클라이언트 애플리케이션 내에서 호출할 수 없습니다.
현재 웹 애플리케이션 디렉터리 수준에 있는 구성 파일에서 구성 섹션을 검색하려면 이 메서드를 GetSection 사용합니다.
메모
이 GetSection 메서드는 애플리케이션이 실행되는 계층 구조 수준에서 구성 파일의 섹션에서 작동하는 런타임 메서드입니다. 런타임이 아닌 작업의 경우 대신 사용합니다 GetSection . 이 메서드는 구성 파일 메서드 중 하나를 사용하여 가져오는 구성 파일의 open 지정된 섹션에서 작동합니다.
상속자 참고
반환 값은 사용하기 전에 예상된 구성 형식으로 캐스팅해야 합니다. 가능한 캐스팅 예외를 방지하려면 C#의 연산자처럼 as 조건부 캐스팅 작업을 사용해야 합니다.