Wednesday, April 12, 2006

How to Add Custom Tag in Web.Config Page (ASP.NET)

How to Add Custom Tag in Web.Config Page (ASP.NET)
To add a custom tag to Web.config you need to first explicitly specify the new tag name in Web.config via the <configSections> tag
For Example:

<configuration>

<configSections>

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

</configSections>
...
</configuration>

This <section ... /> tag indicates that we are going to be adding New tag named MyAppSettings.
Now we can add a <MyAppSettings> tag to our Web.config file and
add <add ... /> tags to add application-wide parameters,
For Example:

<configuration>

<configSections>

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

<MyAppSettings>
<add key="connString" value="connection string" />
</MyAppSettings>
...
</configuration>
How to Read Custom Tag

To read this custom value from an ASP.NET Web page we use the following syntax:

ConfigurationSettings.GetConfig("MyAppSettings")("connString")


Ritesh Kumar Kesharwani
Infosys,Pune

Talk is cheap. Use Yahoo! Messenger to make PC-to-Phone calls.
Great rates starting at 1¢/min.

No comments: