ASP.NET Tutorial/Configuration/appSettings
Содержание
ConfigurationSettings.AppSettings
<%@ Page Language="VB" %>
<html>
<head>
<script runat="server">
Sub Page_Load()
Message1.Text &= ConfigurationSettings.AppSettings("machineConfigKey")
Message2.Text &= ConfigurationSettings.AppSettings("applicationConfigKey")
End Sub
</script>
</head>
<body>
<asp:label id="Message1" runat="server">Machine.Config setting: </asp:label>
<br/>
<asp:label id="Message2" runat="server">Web.Config setting: </asp:label>
</body>
</html>
File: Web.Config
<configuration>
<appSettings>
<add key="applicationConfigKey" value="bar"/>
</appSettings>
<system.web>
<authorization>
<deny users="?"/>
</authorization>
</system.web>
</configuration>
Loading string from Web.config (C#)
File: Web.config
<?xml version="1.0"?>
<configuration>
<appSettings>
<add key="DataFilePath" value="e:\NetworkShare\Documents\WebApp\Shared"/>
</appSettings>
</configuration>
File: ShowSettings.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="ShowSettings.aspx.cs" Inherits="ShowSettings" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form ID="form1" runat="server">
<div>
<asp:Label ID="lblTest" runat="server" />
</div>
</form>
</body>
</html>
File: ShowSettings.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;
public partial class ShowSettings : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
lblTest.Text = "This app will look for data in the directory:<br /><b>";
lblTest.Text += WebConfigurationManager.AppSettings["DataFilePath"];
lblTest.Text += "</b>";
}
}
Retrieve AppSetting
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="RetrieveAppSetting" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<ul>
<li><asp:Label ID="appSettingValue" runat="server" /></li>
<li><asp:Label ID="appSettingsExample" runat="server" Text="<%$ AppSettings:AnotherSetting %>" /></li>
<li><asp:Label ID="connStr" runat="server" /></li>
<li><asp:Label ID="connection" runat="server" Text="<%$ ConnectionStrings:sqlDb %>" /></li>
<li><asp:Label ID="lblMyStuff" runat="server" /></li>
</ul>
<asp:SqlDataSource ID="ds" runat="server" ConnectionString="<%$ ConnectionSTrings:sqlDb %>"></asp:SqlDataSource>
</div>
</form>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Web.Configuration;
public partial class RetrieveAppSetting : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
appSettingValue.Text = ConfigurationManager.AppSettings["MySetting"];
connStr.Text = ConfigurationManager.ConnectionStrings["sqlDb"].ConnectionString;
lblMyStuff.Text = MySettings.Settings.Bar;
}
}
Retrieve values from the appSettings section either programmatically or declaratively.
<%@ Page Language="C#" %>
<%@ Import Namespace="System.Web.Configuration" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<script runat="server">
void Page_Load()
{
lblWelcome.Text = WebConfigurationManager.AppSettings["welcome"];
}
</script>
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Show AppSettings</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label
id="lblWelcome"
Runat="server" />
<hr />
<asp:Literal
id="ltlCopyright"
Text="<%$ AppSettings:copyright %>"
Runat="server" />
</div>
</form>
</body>
</html>
File: Web.config
<configuration>
<appSettings>
<add key="welcome" value="Welcome to our Web site!" />
<add key="copyright" value="Copyright (c) 2007 by the company" />
</appSettings>
</configuration>
Storing Custom Settings in the web.config File
ASP.NET stores your own settings in the web.config file, in an element called <appSettings>.
<?xml version="1.0" ?>
<configuration>
...
<appSettings>
<add key="DataFilePath" value="C:\Shared" />
</appSettings>
...
<system.web>
<!-- ASP.NET Configuration sections go here. -->
</system.web>
...
</configuration>