Share via


如何:以编程方式访问 ASP.NET 配置设置

更新:2007 年 11 月

可以从 ASP.NET 应用程序或 .NET 客户端应用程序内部访问运行时配置设置。每个配置节都具有其各自的对象类型,对于 C#,在调用 WebConfigurationManager 类的方法时,需要对这些对象类型进行强制转换。有关与配置节关联的对象类型的更多信息,请参见 ASP.NET 配置设置下参考主题中“元素信息”表中的节处理程序。

此主题中的代码示例使用获取配置数据的非静态方法。这允许您从任何应用程序获取配置信息。如果要从代码驻留的应用程序中获取配置信息,请使用处理速度更快的静态 GetSection 方法。有关更多信息,请参见 ASP.NET 配置 API 概述中的“使用本地和远程配置设置”一节。

示例

下面的代码示例读取针对 MyAppRoot 应用程序配置的 identity 元素的 impersonate 属性值。该值显示在网页上。该代码使用 IdentitySection 对象类型读取 identity 节中的数据。

若要更改配置设置,请使用配置对象的 SaveSaveAs 方法。有关更多信息,请参见使用配置类

<%@ 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 runat="server">

    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#" runat="server">
    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>

请参见

概念

使用配置类

ASP.NET 配置概述

参考

OpenWebConfiguration

其他资源

配置应用程序

帮助主题:配置 ASP.NET 应用程序