방법: 프로그래밍 방식으로 ASP.NET 구성 설정 사용
업데이트: 2007년 11월
ASP.NET 응용 프로그램이나 .NET 클라이언트 응용 프로그램 내에서 런타임 구성 설정에 액세스할 수 있습니다. 각 구성 섹션에는 자체 개체 형식이 있으며 C#의 경우 WebConfigurationManager 클래스의 메서드를 호출할 때 캐스트가 필요합니다. 구성 섹션과 관련된 개체 형식에 대한 자세한 내용은 ASP.NET 구성 설정의 참조 항목에 있는 요소 정보 표에서 섹션 처리기를 참조하십시오.
이 항목의 코드 예제에서는 비정적 메서드를 사용하여 구성 데이터를 가져옵니다. 이를 통해 모든 응용 프로그램에서 구성 정보를 가져올 수 있습니다. 코드가 있는 응용 프로그램에서 구성 정보를 가져오려면 정적 GetSection 메서드를 사용하는 것이 처리 속도가 빠릅니다. 자세한 내용은 ASP.NET 구성 API 개요에서 로컬 및 원격 구성 설정 사용 단원을 참조하십시오.
예제
다음 코드 예제에서는 MyAppRoot 응용 프로그램에 대해 구성된 identity 요소의 impersonate 특성 값을 읽습니다. 이 값이 웹 페이지에 표시됩니다. 코드에서는 IdentitySection 개체 형식을 사용하여 identity 섹션에 있는 데이터를 읽습니다.
구성 설정을 변경하려면 구성 개체의 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 >
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>