共用方式為


HOW TO:以程式設計方式存取 ASP.NET 組態設定

更新:2007 年 11 月

您可以從 ASP.NET 應用程式或 .NET 用戶端應用程式內存取執行階段組態設定。每個組態區段都擁有本身的物件型別,以 C# 而言,在呼叫 WebConfigurationManager 類別的方法時,需要進行轉型 (Casting)。如需與組態區段相關之物件型別的詳細資訊,請參閱 ASP.NET 組態設定參考主題中項目資訊表的區段處理常式。

這個主題中的程式碼範例會使用非靜態方法取得組態資料。這可讓您從任何應用程式取得組態資訊。如果您要從程式碼所在的應用程式中取得組態資訊,請使用靜態 GetSection 方法,這樣處理速度較快。如需詳細資訊,請參閱 ASP.NET 組態 API 概觀中的<使用本機和遠端組態設定>章節。

範例

下列的程式碼範例讀取針對 MyAppRoot 應用程式所設定的 identity 項目的 impersonate 屬性 (Attribute) 值。這個值會顯示在 Web 網頁上。程式碼會使用 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 >

    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>

請參閱

概念

使用組態類別

ASP.NET 組態概觀

參考

OpenWebConfiguration

其他資源

設定應用程式

HOW TO 主題 - 設定 ASP.NET 應用程式