Thursday, September 07, 2006

Creating Custom App Settings in Web.Config

Most of the application requires lots of settings to be specified in the App Settings. Unless you manage that in a neat way, it will soon become a headache for you.

.Net helps you out in this situation by allowing you to have custom AppSettings in your web.config. This will help you to group your appSettings in a more efficient manner so that management becomes much easier.

In this blog, I have a sample code which lets you to retrieve the values from the custom AppSettings tag in your web.config.

The Code....


// Usage
string MyKey = GetSettings ("Mykey");

// The method
private string GetSettings(string Key)
{
System.Collections.Specialized.NameValueCollection obj = (System.Collections.Specialized.NameValueCollection) System.Configuration.ConfigurationSettings.GetConfig("MySettings");
return obj[Key].ToString();
}



The Web.config...

<!-- Defining a new section -->

<configSections>
<section name="MySettings"
type="System.Configuration.NameValueFileSectionHandler,System, Version=1.0.3300.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" />
</configSections>

<!-- The New Section -->

<MySettings>
<add key="Mykey" value="MyKeyValue" />
</MySettings>

0 Comments:

Post a Comment

<< Home