WebConfigurationManager.GetSection 메서드

정의

현재 웹 애플리케이션의 기본 구성 파일에서 지정된 구성 섹션을 검색합니다.

오버로드

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

구성 섹션 이름입니다.

반환

Object

지정된 구성 섹션 개체이거나, 섹션이 없는 경우 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 라고에서 웹 애플리케이션 내에서 가져와서 웹 애플리케이션 구성 계층 구조에 따라 시스템에서 선택한 구성 파일에서 섹션입니다.

주의

애플리케이션에서 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

가상 구성 파일 경로입니다.

반환

Object

지정된 구성 섹션 개체이거나, 섹션이 없는 경우 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 라고에서 내 웹 애플리케이션을 가져와서 구성 계층 구조에서 지정 된 경로 의해 정의 된 구성 파일에서 섹션입니다.

주의

애플리케이션에서 HTTP 끝점과 다른 프로토콜을 사용 하는 경우는 GetSection 섹션 이름과 경로 모두 해당 매개 변수 목록에는 오버 로드는 사용 하도록 합니다. 시스템 구성 계층 구조 수준에 대 한 어떠한가 정도 할 수 없습니다 때문에 구성 파일 경로 지정 해야 합니다. 사용 하는 경우는 GetSection 섹션 이름만, 시스템이 오버 로드는 항상 하려고 애플리케이션 수준 구성 설정을 반환 합니다. 단, 지정된 된 경로가 현재 애플리케이션 외부의 경우는 오버 로드는 경로 반환 한다는 것도 현재 실행 중인 애플리케이션에 대 한 애플리케이션 수준 구성 합니다.

이 메서드는 클라이언트 애플리케이션 내에서 호출할 수 없습니다.

수준에 있는 현재 웹 애플리케이션 디렉터리를 사용 하 여 구성 파일에서 구성 섹션을 검색 하려는 경우는 GetSection 메서드.

참고

GetSection 애플리케이션이 실행 되는 계층 구조 수준에서 구성 파일의 섹션에서 작동 하는 런타임 메서드입니다. 실행 시간이 아닌 작업을 사용 하 여 GetSection 대신 합니다. 이 메서드 중 하나를 사용 하 여 가져와야 하는 구성 파일의 지정된 된 섹션에서 작동 합니다 open 구성 파일 메서드.

상속자 참고

반환 값을 사용 하기 전에 예상 되는 구성 형식으로 캐스팅 되어야 합니다. 가능한 캐스팅 예외를 방지하려면 C#의 연산자처럼 as 조건부 캐스팅 작업을 사용해야 합니다.

추가 정보

적용 대상