ASP.NET AppSettings in Web.Config Example

Leave a Comment
In this article, we have to learn how to add custom AppSettings in Web.Config file and how to access value defined in Web.Config AppSettings Section in ASP.NET.

First Defined AppSettings Section in Web.Config file as shown below code:

Web.Config
<configuration>
  <appSettings>
    <add key="backgroundColor" value="Green"/>
  </appSettings>
  .......
  .......
</configuration>

Now to access above AppSettings key "backgroundColor" use below code:

Default.aspx
<form id="form1" runat="server">
    <div>
        <h2>AppSettings</h2>
        <asp:Label ID="appSettingsLabel" runat="server" />
    </div>
    </form>

Default.aspx.cs
using System.Configuration;
namespace CustomConfigSettingDemo
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            appSettingsLabel.Text = ConfigurationManager.AppSettings["BackgroundColor"];
        }
    }
}

0 comments:

Post a Comment