方法 : プログラムによって ASP.NET 構成設定にアクセスする
更新 : 2007 年 11 月
ASP.NET アプリケーションまたは .NET クライアント アプリケーション内から実行時の構成設定にアクセスできます。各構成セクションは独自のオブジェクト型を持ち、WebConfigurationManager クラスのメソッドを呼び出す場合に C# ではキャストを必要とします。構成セクションに関連するオブジェクトの型の詳細については、「ASP.NET 構成設定」の参照トピックにある要素情報の表でセクション ハンドラを参照してください。
このトピックのコード例では、構成データを取得する非静的メソッドを使用します。これにより、任意のアプリケーションから構成情報を取得できます。コードが存在するアプリケーションから構成情報を取得する場合は、処理のより高速な GetSection 静的メソッドを使用します。詳細については、「ASP.NET 構成 API の概要」の「ローカルの構成設定およびリモートの構成設定の操作」を参照してください。
使用例
次のコード例では、MyAppRoot アプリケーション向けに構成された identity 要素の impersonate 属性の値を読み取っています。この値は、Web ページ上に表示されます。このコードは、identity セクション内のデータを読み取るために IdentitySection オブジェクト型を使用しています。
構成設定を変更するには、構成オブジェクトの Save メソッドまたは SaveAs メソッドを使用します。詳細については、「構成クラスの使用」を参照してください。
<%@ 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>