Share via


Comment : accéder par programme aux paramètres de configuration ASP.NET

Mise à jour : novembre 2007

Vous pouvez accéder aux paramètres de configuration à l'exécution à partir d'une application ASP.NET ou d'une application cliente .NET. Chaque section de configuration possède son propre type d'objet qui, dans le cas de C#, exige un cast lors de l'appel des méthodes de la classe WebConfigurationManager. Pour plus d'informations sur le type d'objet associé à une section de configuration, consultez le gestionnaire de section dans le tableau Informations sur les éléments dans les rubriques de référence sous Paramètres de configuration ASP.NET.

L'exemple de code de cette rubrique utilise la méthode non statique pour obtenir les données de configuration. Cela vous permet d'obtenir des informations de configuration d'une application quelconque. Si vous souhaitez obtenir des informations de configuration de l'application dans laquelle votre code réside, utilisez les méthodes GetSection statiques, plus rapides en termes de traitement. Pour plus d'informations, consultez la section Manipulation des paramètres de configuration locaux et distants dans Vue d'ensemble de l'API de configuration ASP.NET.

Exemple

L'exemple de code suivant lit la valeur de l'attribut impersonate de l'élément identity configuré pour l'application MyAppRoot. La valeur est affichée sur la page Web. Le code utilise le type d'objet IdentitySection pour lire les données figurant dans la section identity.

Pour modifier un paramètre de configuration, utilisez la méthode Save ou SaveAs de l'objet de configuration. Pour plus d'informations, consultez Utilisation des classes de configuration.

<%@ Page Language="VB" %>

<%@ Import Namespace="System.Web.Configuration" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Text" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script >

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

        ' Set the root path of the Web application that contains the
        ' Web.config file that you want to access.
        Dim configPath As String = "/MyAppRoot"

        ' Get the configuration object to access the related Web.config file.
        Dim config As Configuration = WebConfigurationManager.OpenWebConfiguration(configPath)

        ' Get the object related to the <identity> section.
        Dim section As New IdentitySection
        section = config.GetSection("system.web/identity")

        ' Read the <identity> section.
        Dim identity As New StringBuilder
        identity.Append("Impersonate: ")
        identity.Append(section.Impersonate.ToString())

        ' Display the <identity> information.
        ConfigId.Text = identity.ToString()

    End Sub
</script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Read Configuration Settings</title>
</head>
<body>
    <h2>
        Read ASP.NET Configuration</h2>
    <p>
        This page displays the value of the <b>Impersonate</b> attribute of the <b>identity</b>
        section of an ASP.NET configuration.
    </p>
    <h3>
        Results</h3>
    <p>
        <asp:Label ID="ConfigId" BackColor="#dcdcdc" BorderWidth="1" runat="Server" /></p>
</body>
</html>
<%@ Page Language="C#" %>

<%@ Import Namespace="System.Web.Configuration" %>
<%@ Import Namespace="System.Configuration" %>
<%@ Import Namespace="System.Text" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script language="C#" >
    public void Page_Load()
    {
        try
        {
            // Set the root path of the Web application that contains the
            // Web.config file that you want to access.
            string configPath = "/MyAppRoot";

            // Get the configuration object to access the related Web.config file.
            Configuration config = WebConfigurationManager.OpenWebConfiguration(configPath);

            // Get the object related to the <identity> section.
            IdentitySection section =
              (IdentitySection)config.GetSection("system.web/identity");

            // Read the <identity> section.
            StringBuilder identity = new StringBuilder();
            identity.Append("Impersonate: ");
            identity.Append(section.Impersonate.ToString());

            // Display the <identity> information.
            ConfigId.Text = identity.ToString();
        }
        catch (Exception e)
        {
            ConfigId.Text = e.ToString();
        }
    }
  </script>

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>Read Configuration Settings</title>
</head>
<body>
    <h2>
        Read ASP.NET Configuration</h2>
    <p>
        This page displays the value of the <b>Impersonate</b> attribute of the <b>identity</b>
        section of an ASP.NET configuration.
    </p>
    <h3>
        Results</h3>
    <p>
        <asp:Label ID="ConfigId" BackColor="#dcdcdc" BorderWidth="1" runat="Server" /></p>
</body>
</html>

Voir aussi

Concepts

Utilisation des classes de configuration

Vue d'ensemble de la configuration ASP.NET

Référence

OpenWebConfiguration

Autres ressources

Configuration d'applications

Rubriques Comment — configuration d'applications ASP.NET