Edit

Share via


WebConfigurationManager.GetSection Method

Definition

Retrieves the specified configuration section from the current Web application's default configuration file.

Overloads

GetSection(String)

Retrieves the specified configuration section from the current Web application's configuration file.

GetSection(String, String)

Retrieves the specified configuration section from the Web application's configuration file at the specified location.

GetSection(String)

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

Parameters

sectionName
String

The configuration section name.

Returns

The specified configuration section object, or null if the section does not exist. 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.

Exceptions

A valid configuration file could not be loaded.

Examples

The examples in this section show how to access configuration information with the GetSection method.

The following example shows a section that can be accessed from either a Web application or a console application.

Note

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

Remarks

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.

Caution

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

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

Note

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. For a non-run-time operation, use GetSection instead. 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.

Notes to Inheritors

The return value must be cast to the expected configuration type before use. To avoid possible casting exceptions, you should use a conditional casting operation like the as operator in C#.

See also

Applies to

GetSection(String, String)

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

Parameters

sectionName
String

The configuration section name.

path
String

The virtual configuration file path.

Returns

The specified configuration section object, or null if the section does not exist. 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.

Exceptions

The method is called from outside a Web application.

A valid configuration file could not be loaded.

Examples

The following example shows how to access configuration information with the GetSection method.

Note

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

Remarks

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.

Caution

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

If you want to retrieve the configuration section from the configuration file located at the current Web application directory level, use the GetSection method.

Note

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. For a non-run-time operation, use GetSection instead. This method operates on the specified section of a configuration file that you obtain using one of the open configuration file methods.

Notes to Inheritors

The return value must be cast to the expected configuration type before use. To avoid possible casting exceptions, you should use a conditional casting operation like the as operator in C#.

See also

Applies to