WebConfigurationManager.GetWebApplicationSection(String) Methode

Definitie

Haalt de opgegeven configuratiesectie op uit het configuratiebestand van de huidige webtoepassing.

public:
 static System::Object ^ GetWebApplicationSection(System::String ^ sectionName);
public static object GetWebApplicationSection(string sectionName);
static member GetWebApplicationSection : string -> obj
Public Shared Function GetWebApplicationSection (sectionName As String) As Object

Parameters

sectionName
String

De naam van de configuratiesectie.

Retouren

Het opgegeven configuratiesectieobject, of null als de sectie niet bestaat, of een intern object als de sectie tijdens runtime niet toegankelijk is.

Uitzonderingen

Er kan geen geldig configuratiebestand worden geladen.

Voorbeelden

In het volgende voorbeeld ziet u hoe u met de GetWebApplicationSection methode toegang hebt tot configuratiegegevens.

Note

In dit voorbeeld ziet u hoe u de GetWebApplicationSection methode gebruikt om een ConfigurationSection object op te halen uit het standaardconfiguratiebestand.


// Show the use of GetWebApplicationSection(string).
// to get the connectionStrings section.
 static void GetWebApplicationSection()
 {

     // Get the default connectionStrings section,
     ConnectionStringsSection connectionStringsSection =
         WebConfigurationManager.GetWebApplicationSection(
         "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 connectionStrings]");
     while (connectionStringsEnum.MoveNext())
     {
         string name = connectionStrings[i].Name;
         Console.WriteLine("Name: {0} Value: {1}",
         name, connectionStrings[name]);
         i += 1;
     }

     Console.WriteLine();
 }
' Show the use of GetWebApplicationSection(string). 
' to access the connectionStrings section.
Shared Sub GetWebApplicationSection()
   
   ' Get the default connectionStrings section,
     Dim connectionStringsSection As ConnectionStringsSection = _
     WebConfigurationManager.GetWebApplicationSection( _
     "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 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

Opmerkingen

Als GetWebApplicationSection deze wordt aangeroepen vanuit een webtoepassing, wordt de sectie opgehaald uit het configuratiebestand dat door het systeem is geselecteerd volgens de configuratiehiƫrarchie van de webtoepassing.

U kunt vanuit een clienttoepassing bellen GetWebApplicationSection . In dit geval wordt de standaardsectie opgehaald uit het configuratiebestand dat door het systeem is geselecteerd volgens de clientconfiguratiehiƫrarchie. Dit is meestal het Machine.config bestand, tenzij u een toegewezen configuratie hebt. Raadpleeg de toewijzingsmethoden die hierna worden beschreven voor toewijzingsconfiguratiebestanden.

Note

De GetWebApplicationSection methode is een runtimebewerking die op het huidige niveau op het gedeelte van het toepassingsconfiguratiebestand reageert. De GetSection methode is echter geen runtime-bewerking, maar fungeert op de opgegeven sectie die is verkregen via een van de methoden voor het openen van de configuratiebestanden.

Notities voor overnemers

De retourwaarde moet worden gecast naar het verwachte configuratietype voordat u deze gebruikt. Als u mogelijke cast-uitzonderingen wilt voorkomen, moet u een voorwaardelijke cast-bewerking gebruiken, zoals de as operator in C#.

Van toepassing op

Zie ook