GetSection Method (String)
Collapse the table of content
Expand the table of content

WebConfigurationManager.GetSection Method (String)

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

Namespace: System.Web.Configuration
Assembly: System.Web (in system.web.dll)

public static Object GetSection (
	string sectionName
)
public static Object GetSection (
	String sectionName
)
public static function GetSection (
	sectionName : String
) : Object
Not applicable.

Parameters

sectionName

The configuration section name.

Return Value

The specified configuration section object, or a null reference (Nothing in Visual Basic) if the section does not exist. Remember that security restrictions exist on the use of GetSection as a runtime operation. You might not be able to access a section at run time for modifications, for example.

Exception typeCondition

ConfigurationErrorsException

A valid configuration file could not be loaded.

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 noteCaution:

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.

NoteNote:

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 Implementers: 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# or the TryCast function in Visual Basic.

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.


// Show the use of GetSection(string). 
// It gets the connectiobStrings section.
// If called from within a client application,
// the GetSection(string) gets the default connectionStrings
// section from the machine.config.
// If called from within a Web aplication it gets the
// section from the configuration file located at the 
// application current level. 
static void GetSection1()
{

    // 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();
}


The following examples show sections that can be accessed only from a Web application.


// Show the use of GetSection(string).
// It gets the appSettings section.
// It can only be called from within a Web application. 
// Note. If you run this method from a console app  
// you get a NullReferenceException.
static void GetSection19()
{

    try
    {
        // Get the appSettings section.
        AppSettingsSection appSettingsSection =
            WebConfigurationManager.GetSection("appSettings")
            as AppSettingsSection;

        // Get the appSettings key,value pairs collection.
    KeyValueConfigurationCollection appSettings =
        appSettingsSection.Settings;

    // Get the collection enumerator.
    IEnumerator appSettingsEnum =
        appSettings.GetEnumerator();

    // Loop through the collection and 
    // display the appSettings key, value pairs.
    int i = 0;
    Console.WriteLine("[Display the appSettings]");
    while (appSettingsEnum.MoveNext())
    {
        string key = appSettings.AllKeys[i].ToString();
        Console.WriteLine("Key: {0} Value: {1}",
        key, appSettings[key]);
        i += 1;
    }

    Console.WriteLine();

}

catch (NullReferenceException e)
    {
        Console.WriteLine(e.ToString());
    }
}



// Show the use of GetSection(string).
// It can only be used from within a Web application. 
// It gets the identity section that is 
// specific to a Web application (that is a section
// contained in the system.web group). 
// If you run this method from a console
// app you get a NullReferenceException.
static void GetSection20()
{

    try
    {
        // Get the identitySection section.
        IdentitySection identitySection =
            WebConfigurationManager.GetSection("identity")
            as IdentitySection;

        Console.WriteLine("[Display the identity]");
       
        Console.WriteLine("Impersonate: {0}", 
            identitySection.Impersonate.ToString());
        
        Console.WriteLine();

    }

    catch (NullReferenceException e)
    {
        Console.WriteLine(e.ToString());
    }
}


Windows 98, Windows Server 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition

The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.

.NET Framework

Supported in: 3.0, 2.0

Community Additions

ADD
Show:
© 2016 Microsoft