ASP.NET Tutorial/Configuration/appSettings

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

ConfigurationSettings.AppSettings

   <source lang="csharp">

<%@ 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>
  
<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></source>


Loading string from Web.config (C#)

   <source lang="csharp">

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">
     <asp:Label ID="lblTest" runat="server" />    
   </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:
"; lblTest.Text += WebConfigurationManager.AppSettings["DataFilePath"]; lblTest.Text += ""; }

}</source>


Retrieve AppSetting

   <source lang="csharp">

<%@ 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">
  • <asp:Label ID="appSettingValue" runat="server" />
  • <asp:Label ID="appSettingsExample" runat="server" Text="<%$ AppSettings:AnotherSetting %>" />
  • <asp:Label ID="connStr" runat="server" />
  • <asp:Label ID="connection" runat="server" Text="<%$ ConnectionStrings:sqlDb %>" />
  • <asp:Label ID="lblMyStuff" runat="server" />
   <asp:SqlDataSource ID="ds" runat="server" ConnectionString="<%$ ConnectionSTrings:sqlDb %>"></asp:SqlDataSource>
   </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;
 }

}</source>


Retrieve values from the appSettings section either programmatically or declaratively.

   <source lang="csharp">

<%@ 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">
   <asp:Label
       id="lblWelcome"
       Runat="server" />

   <asp:Literal
       id="ltlCopyright"
       Text="<%$ AppSettings:copyright %>"
       Runat="server" />
   </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></source>


Storing Custom Settings in the web.config File

   <source lang="csharp">

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>
   
   </system.web>
   ...

</configuration></source>