Custom settings from web.config

Hi,

In this example I’ll show you how you can get custom settings from the web.config file in your ASP.NET project. You can add as many custom settings as you want.

First you open your web.config and search for the <appSettings> tag. Here you use the add tag. You have to specify a specific key and give it a value. This looks like this:

<appSettings> <add key="logDir" value="c:\logs"/> </appSettings>

Then you can call this keyed value in your ASP.NET page like this:

protected void Page_Load(object sender, EventArgs e) { Result.InnerText = "The path of the log directory: " + WebConfigurationManager.AppSettings["logDir"]; }

WebConfigurationManager needs this namespace:

using System.Web.Configuration;

So don’t forget to add that.


No comments yet. Be the first.

Leave a reply

*
To prove you're a person (not a spam script), type the security word shown in the picture. Click on the picture to hear an audio file of the word.
Click to hear an audio file of the anti-spam word