WebConfigurationManager.GetSection WebConfigurationManager.GetSection WebConfigurationManager.GetSection WebConfigurationManager.GetSection Method

定义

从当前 Web 应用程序的默认配置文件中检索指定的配置节。Retrieves the specified configuration section from the current Web application's default configuration file.

重载

GetSection(String) GetSection(String) GetSection(String) GetSection(String)

从当前 Web 应用程序的配置文件中检索指定的配置节。Retrieves the specified configuration section from the current Web application's configuration file.

GetSection(String, String) GetSection(String, String) GetSection(String, String) GetSection(String, String)

从指定的 Web 应用程序的配置文件中的指定位置检索指定的配置节。Retrieves the specified configuration section from the Web application's configuration file at the specified location.

GetSection(String) GetSection(String) GetSection(String) GetSection(String)

从当前 Web 应用程序的配置文件中检索指定的配置节。Retrieves the specified configuration section from the current Web application's configuration file.

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 String String String

配置节名称。The configuration section name.

返回

指定的配置节对象或 null(如果该节不存在)。The specified configuration section object, or null if the section does not exist. 请记住,将 GetSection(String) 用作运行时操作存在安全限制。Remember that security restrictions exist on the use of GetSection(String) as a runtime operation. 例如,您可能无法在运行时访问某节以进行修改。You might not be able to access a section at run time for modifications, for example.

异常

未能加载有效的配置文件。A valid configuration file could not be loaded.

示例

本节中的示例演示如何使用GetSection方法访问配置信息。The examples in this section show how to access configuration information with the GetSection method.

下面的示例演示了一个可从 Web 应用程序或控制台应用程序访问的部分。The following example shows a section that can be accessed from either a Web application or a console application.

备注

此示例演示如何使用GetWebApplicationSection方法从配置文件中ConfigurationSection获取对象。This example demonstrates how to use the GetWebApplicationSection method to get a ConfigurationSection object from a configuration file.


// 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 应用程序配置层次结构选择的配置文件中获取部分。If GetSection is called from within a Web application, it gets the section from the configuration file selected by the system according to the Web-application configuration hierarchy.

注意

如果你的应用程序使用的协议不同于 HTTP GetSection , 则在其参数列表中采用节名称和路径的重载是要使用的协议。If your application uses a different protocol than HTTP, the GetSection overload that takes both a section name and a path in its parameter list is the one to use. 您必须指定配置文件路径, 因为系统无法对配置层次结构级别作出任何假设。You must specify the configuration file path because the system cannot make any assumptions about the configuration hierarchy level. 如果使用GetSection只采用部分名称的重载, 则系统将始终尝试在应用程序级别返回配置设置。If you use the GetSection overload that takes only a section name, the system will always attempt to return the configuration settings at the application level. 但请注意, 如果其指定的路径不在当前应用程序的范围内, 则采用路径的重载也将返回当前正在运行的应用程序的应用程序级别的配置设置。Note, though, that if its specified path is outside of the current application, the overload that takes a path will also return the application-level configuration settings for the currently running application.

你可以从GetSection客户端应用程序中调用。You could call GetSection from within a client application. 在这种情况下, 它会根据客户端配置层次结构从系统所选的配置文件中获取默认节。In this case, it gets the default section from the configuration file selected by the system according to the client configuration hierarchy. 通常, 这是 machine.config 文件, 除非已有一个已有的映射配置。Usually, this is the Machine.config file, unless you have a mapped configuration in place. 有关映射配置文件的详细说明, 请参阅下一步所述的映射方法。For mapping configuration files, refer to the mapping methods described next.

备注

GetSection方法是一种运行时方法, 可对应用程序运行时所在的层次结构级别的配置文件的部分进行操作。The GetSection method is a run-time method that operates on the section of a configuration file at the hierarchy level in which the application runs. 对于非运行时操作, 请GetSection改用。For a non-run-time operation, use GetSection instead. 此方法在配置文件的指定节上操作, 该配置文件使用一种用于打开配置文件OpenWebConfiguration的重载方法获取。This method operates on the specified section of a configuration file that you obtain using one of the overloaded methods for opening a configuration file, OpenWebConfiguration.

继承者说明

在使用之前, 必须将返回值强制转换为所需的配置类型。The return value must be cast to the expected configuration type before use. 若要避免可能出现的强制转换异常, 应在中as C#使用运算符 (如运算符) 或函数 (Visual Basic 中的函数)。To avoid possible casting exceptions, you should use a conditional casting operation like the as operator in C# or the function in Visual Basic.

另请参阅

GetSection(String, String) GetSection(String, String) GetSection(String, String) GetSection(String, String)

从指定的 Web 应用程序的配置文件中的指定位置检索指定的配置节。Retrieves the specified configuration section from the Web application's configuration file at the specified location.

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 String String String

配置节名称。The configuration section name.

path
String String String String

虚拟配置文件路径。The virtual configuration file path.

返回

指定的配置节对象或 null(如果该节不存在)。The specified configuration section object, or null if the section does not exist. 请记住,将 GetSection(String, String) 用作运行时操作存在安全限制。Remember that security restrictions exist on the use of GetSection(String, String) as a run-time operation. 例如,您可能无法在运行时访问某节以进行修改。You might not be able to access a section at run time for modifications, for instance.

异常

从 Web 应用程序外调用方法。The method is called from outside a Web application.

未能加载有效的配置文件。A valid configuration file could not be loaded.

示例

下面的示例演示如何使用GetSection方法访问配置信息。The following example shows how to access configuration information with the GetSection method.

备注

此示例演示如何使用GetSection方法从指定的配置文件中获取一个ConfigurationSection对象。This example demonstrates how to use the GetSection method to get a ConfigurationSection object from a specified configuration file.


// 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 应用程序中调用, 它将从配置层次结构中由指定路径定义的配置文件中获取部分。If GetSection is called from within a Web application, it gets the section from the configuration file defined by the specified path in the configuration hierarchy.

注意

如果你的应用程序使用的协议不同于 HTTP GetSection , 则在其参数列表中采用节名称和路径的重载是要使用的协议。If your application uses a different protocol than HTTP, the GetSection overload that takes both a section name and a path in its parameter list is the one to use. 您必须指定配置文件路径, 因为系统无法对配置层次结构级别作出任何假设。You must specify the configuration file path because the system cannot make any assumptions about the configuration hierarchy level. 如果使用GetSection只采用部分名称的重载, 则系统将始终尝试在应用程序级别返回配置设置。If you use the GetSection overload that takes only a section name, the system will always attempt to return the configuration settings at the application level. 但请注意, 如果其指定的路径不在当前应用程序的范围内, 则采用路径的重载也将返回当前正在运行的应用程序的应用程序级别的配置设置。Note, though, that if its specified path is outside of the current application, the overload that takes a path will also return the application-level configuration settings for the currently running application.

不能从客户端应用程序中调用此方法。This method cannot be called from within a client application.

如果要从位于当前 Web 应用程序目录级别的配置文件中检索配置节, 请使用GetSection方法。If you want to retrieve the configuration section from the configuration file located at the current Web application directory level, use the GetSection method.

备注

GetSection方法是在应用程序运行的层次结构级别上的配置文件部分中操作的运行时方法。The GetSection method is a run-time method operating on the section of a configuration file at the hierarchy level in which the application runs. 对于非运行时操作, 请GetSection改用。For a non-run-time operation, use GetSection instead. 此方法在使用open配置文件方法之一获取的配置文件的指定节上操作。This method operates on the specified section of a configuration file that you obtain using one of the open configuration file methods.

继承者说明

在使用之前, 必须将返回值强制转换为所需的配置类型。The return value must be cast to the expected configuration type before use. 若要避免可能出现的强制转换异常, 应在中as C#使用运算符 (如运算符) 或函数 (Visual Basic 中的函数)。To avoid possible casting exceptions, you should use a conditional casting operation like the as operator in C# or the function in Visual Basic.

另请参阅

适用于