471,354 Members | 2,090 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,354 software developers and data experts.

Custom section in web.config

Hi,

I want to create a custom section in my web.config that can hold my custom
values. I created a section in web.config as written below.

<configSections>
<section name="myCustomSection"
type="ComIT.Applications.Common.ConfigurationSecti ons.MyHandler"
allowLocation="true" allowDefinition="Everywhere" />
</configSections>

<myCustomSection>
<add key="LastErrorKey1" value="LastError" />
<add key="LastErrorKey2" value="LastError" />
<add key="LastErrorKey3" value="LastError" />
<add key="LastErrorKey4" value="LastError" />
...
</myCustomSection>

Here is my MyHandler class

namespace ComIT.Applications.Common.ConfigurationSections
{
public class MyHandler : ConfigurationSection
{
public MyHandler()
{
}
}
}

I use this code to get my section.
MyHandler config =
(MyHandler)System.Configuration.ConfigurationManag er.GetSection("myCustomSection");

This gives me following error:
Unrecognized element 'add'. (c:\\Inetpub\\wwwroot\\MedTime\\web.config line
21).

I searched from msdn. I found this topic
http://msdn2.microsoft.com/en-us/library/2tw134k3.aspx. This does not fulfill
my requirement either.

I need a simple section like built-in <appSettings> og <connectionStrings>
section. How can I do this?

Cann’t I define a property to my class MyHandler as
System.Collections.Specialized.NameValueCollection that can hold all the
values under my section like ConfigurationManager.AppSettings.
Aftab Ahmad
aa@com-it.dk

Nov 20 '05 #1
6 3667
Hi Tabi,

Use System.Configuration.NameValueSectionHandler, it exactly does what you
want

rgrds
--
Bindesh Vijayan
MCP [ASP.NET]
"Tabi" wrote:
Hi,

I want to create a custom section in my web.config that can hold my custom
values. I created a section in web.config as written below.

<configSections>
<section name="myCustomSection"
type="ComIT.Applications.Common.ConfigurationSecti ons.MyHandler"
allowLocation="true" allowDefinition="Everywhere" />
</configSections>

<myCustomSection>
<add key="LastErrorKey1" value="LastError" />
<add key="LastErrorKey2" value="LastError" />
<add key="LastErrorKey3" value="LastError" />
<add key="LastErrorKey4" value="LastError" />
...
</myCustomSection>

Here is my MyHandler class

namespace ComIT.Applications.Common.ConfigurationSections
{
public class MyHandler : ConfigurationSection
{
public MyHandler()
{
}
}
}

I use this code to get my section.
MyHandler config =
(MyHandler)System.Configuration.ConfigurationManag er.GetSection("myCustomSection");

This gives me following error:
Unrecognized element 'add'. (c:\\Inetpub\\wwwroot\\MedTime\\web.config line
21).

I searched from msdn. I found this topic
http://msdn2.microsoft.com/en-us/library/2tw134k3.aspx. This does not fulfill
my requirement either.

I need a simple section like built-in <appSettings> og <connectionStrings>
section. How can I do this?

Cann’t I define a property to my class MyHandler as
System.Collections.Specialized.NameValueCollection that can hold all the
values under my section like ConfigurationManager.AppSettings.
Aftab Ahmad
aa@com-it.dk

Nov 20 '05 #2
Hi binvij,

Have you any example for that?

Tabi

"binvij" wrote:
Hi Tabi,

Use System.Configuration.NameValueSectionHandler, it exactly does what you
want

rgrds
--
Bindesh Vijayan
MCP [ASP.NET]
"Tabi" wrote:
Hi,

I want to create a custom section in my web.config that can hold my custom
values. I created a section in web.config as written below.

<configSections>
<section name="myCustomSection"
type="ComIT.Applications.Common.ConfigurationSecti ons.MyHandler"
allowLocation="true" allowDefinition="Everywhere" />
</configSections>

<myCustomSection>
<add key="LastErrorKey1" value="LastError" />
<add key="LastErrorKey2" value="LastError" />
<add key="LastErrorKey3" value="LastError" />
<add key="LastErrorKey4" value="LastError" />
...
</myCustomSection>

Here is my MyHandler class

namespace ComIT.Applications.Common.ConfigurationSections
{
public class MyHandler : ConfigurationSection
{
public MyHandler()
{
}
}
}

I use this code to get my section.
MyHandler config =
(MyHandler)System.Configuration.ConfigurationManag er.GetSection("myCustomSection");

This gives me following error:
Unrecognized element 'add'. (c:\\Inetpub\\wwwroot\\MedTime\\web.config line
21).

I searched from msdn. I found this topic
http://msdn2.microsoft.com/en-us/library/2tw134k3.aspx. This does not fulfill
my requirement either.

I need a simple section like built-in <appSettings> og <connectionStrings>
section. How can I do this?

Cann’t I define a property to my class MyHandler as
System.Collections.Specialized.NameValueCollection that can hold all the
values under my section like ConfigurationManager.AppSettings.
Aftab Ahmad
aa@com-it.dk

Nov 20 '05 #3
This used to be real easy in 1.1 :

<configuration>
<configSections>
<sectionGroup name="system.web">
<section name="customSection"
type="System.Configuration.NameValueSectionHandler ,
System, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"/>
</configSections>
<customSection>
<add key="customSection Key 1" value="Value for customSection Key 1"/>
<add key="customSection Key 2" value="Value for customSection Key 2"/>
</customSection>
</configuration>

See if :
http://msdn2.microsoft.com/en-us/library/ms228062.aspx
sheds any more light on this for ASP.NET 2.0.

Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
ASPNETFAQ.COM : http://www.aspnetfaq.com/
Foros de ASP.NET en Espaol : http://asp.net.do/foros/
======================================
<Ta**@discussions.microsoft.com> wrote in message
news:53**********************************@microsof t.com...
Hi,

I want to create a custom section in my web.config that can hold my custom
values. I created a section in web.config as written below.

<configSections>
<section name="myCustomSection"
type="ComIT.Applications.Common.ConfigurationSecti ons.MyHandler"
allowLocation="true" allowDefinition="Everywhere" />
</configSections>

<myCustomSection>
<add key="LastErrorKey1" value="LastError" />
<add key="LastErrorKey2" value="LastError" />
<add key="LastErrorKey3" value="LastError" />
<add key="LastErrorKey4" value="LastError" />
...
</myCustomSection>

Here is my MyHandler class

namespace ComIT.Applications.Common.ConfigurationSections
{
public class MyHandler : ConfigurationSection
{
public MyHandler()
{
}
}
}

I use this code to get my section.
MyHandler config =
(MyHandler)System.Configuration.ConfigurationManag er.GetSection("myCustomSection");

This gives me following error:
Unrecognized element 'add'. (c:\\Inetpub\\wwwroot\\MedTime\\web.config line
21).

I searched from msdn. I found this topic
http://msdn2.microsoft.com/en-us/library/2tw134k3.aspx. This does not fulfill
my requirement either.

I need a simple section like built-in <appSettings> og <connectionStrings>
section. How can I do this?

Cann't I define a property to my class MyHandler as
System.Collections.Specialized.NameValueCollection that can hold all the
values under my section like ConfigurationManager.AppSettings.
Aftab Ahmad
aa@com-it.dk


Nov 20 '05 #4
Hi Juan,

I have a code for .NET Framework versions 1.0 and 1.1 and I already have
read this topic, but they have also written a note in this topic.

In the .NET Framework versions 1.0 and 1.1, a configuration section handler
had to implement the System.Configuration.IConfigurationSectionHandler
interface, which is now deprecated. However, a code example exists in How to:
Create Custom Configuration Sections Using IConfigurationSectionHandler.

I want a real .NET Framework version 2.0 code. Have you any example for
that? Thanks in advanced.

Aftab Ahmad
"Juan T. Llibre" wrote:
This used to be real easy in 1.1 :

<configuration>
<configSections>
<sectionGroup name="system.web">
<section name="customSection"
type="System.Configuration.NameValueSectionHandler ,
System, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"/>
</configSections>
<customSection>
<add key="customSection Key 1" value="Value for customSection Key 1"/>
<add key="customSection Key 2" value="Value for customSection Key 2"/>
</customSection>
</configuration>

See if :
http://msdn2.microsoft.com/en-us/library/ms228062.aspx
sheds any more light on this for ASP.NET 2.0.

Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
ASPNETFAQ.COM : http://www.aspnetfaq.com/
Foros de ASP.NET en Español : http://asp.net.do/foros/
======================================
<Ta**@discussions.microsoft.com> wrote in message
news:53**********************************@microsof t.com...
Hi,

I want to create a custom section in my web.config that can hold my custom
values. I created a section in web.config as written below.

<configSections>
<section name="myCustomSection"
type="ComIT.Applications.Common.ConfigurationSecti ons.MyHandler"
allowLocation="true" allowDefinition="Everywhere" />
</configSections>

<myCustomSection>
<add key="LastErrorKey1" value="LastError" />
<add key="LastErrorKey2" value="LastError" />
<add key="LastErrorKey3" value="LastError" />
<add key="LastErrorKey4" value="LastError" />
...
</myCustomSection>

Here is my MyHandler class

namespace ComIT.Applications.Common.ConfigurationSections
{
public class MyHandler : ConfigurationSection
{
public MyHandler()
{
}
}
}

I use this code to get my section.
MyHandler config =
(MyHandler)System.Configuration.ConfigurationManag er.GetSection("myCustomSection");

This gives me following error:
Unrecognized element 'add'. (c:\\Inetpub\\wwwroot\\MedTime\\web.config line
21).

I searched from msdn. I found this topic
http://msdn2.microsoft.com/en-us/library/2tw134k3.aspx. This does not fulfill
my requirement either.

I need a simple section like built-in <appSettings> og <connectionStrings>
section. How can I do this?

Cann't I define a property to my class MyHandler as
System.Collections.Specialized.NameValueCollection that can hold all the
values under my section like ConfigurationManager.AppSettings.
Aftab Ahmad
aa@com-it.dk



Nov 20 '05 #5
Maybe I'm reading that page wrong, but just because it says that
"a configuration section handler had to implement the
System.Configuration.IConfigurationSectionHandler interface,
which is now deprecated" doesn't mean that page shows how to use
the System.Configuration.IConfigurationSectionHandler interface.

In fact, it doesn't.

The sample for 1.0/1.1 is at :
http://msdn2.microsoft.com/en-us/library/ms228056.aspx
and is a totally different coding method than what
http://msdn2.microsoft.com/en-us/library/ms228062.aspx
and the links on that page demonstrate.

See, in :
http://msdn2.microsoft.com/en-us/library/2tw134k3.aspx
the sections titled :

"To create a custom configuration section handler"
"To add a custom section handler to an ASP.NET configuration file"
"To programmatically access your custom configuration data"

Those do *not* use "IConfigurationSectionHandler"; they use
"System.Configuration.ConfigurationSection", a 2.0 class.

See this reference:
http://msdn2.microsoft.com/en-us/lib...onsection.aspx

There's example code on that page.


Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
ASPNETFAQ.COM : http://www.aspnetfaq.com/
Foros de ASP.NET en Espaol : http://asp.net.do/foros/
======================================
"Tabi" <Ta**@discussions.microsoft.com> wrote in message
news:AC**********************************@microsof t.com...
Hi Juan,

I have a code for .NET Framework versions 1.0 and 1.1 and I already have
read this topic, but they have also written a note in this topic.

In the .NET Framework versions 1.0 and 1.1, a configuration section handler
had to implement the System.Configuration.IConfigurationSectionHandler
interface, which is now deprecated. However, a code example exists in How to:
Create Custom Configuration Sections Using IConfigurationSectionHandler.

I want a real .NET Framework version 2.0 code. Have you any example for
that? Thanks in advanced.

Aftab Ahmad
"Juan T. Llibre" wrote:
This used to be real easy in 1.1 :

<configuration>
<configSections>
<sectionGroup name="system.web">
<section name="customSection"
type="System.Configuration.NameValueSectionHandler ,
System, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"/>
</configSections>
<customSection>
<add key="customSection Key 1" value="Value for customSection Key 1"/>
<add key="customSection Key 2" value="Value for customSection Key 2"/>
</customSection>
</configuration>

See if :
http://msdn2.microsoft.com/en-us/library/ms228062.aspx
sheds any more light on this for ASP.NET 2.0.

Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
ASPNETFAQ.COM : http://www.aspnetfaq.com/
Foros de ASP.NET en Espaol : http://asp.net.do/foros/
======================================
<Ta**@discussions.microsoft.com> wrote in message
news:53**********************************@microsof t.com...
> Hi,
>
> I want to create a custom section in my web.config that can hold my custom
> values. I created a section in web.config as written below.
>
> <configSections>
> <section name="myCustomSection"
> type="ComIT.Applications.Common.ConfigurationSecti ons.MyHandler"
> allowLocation="true" allowDefinition="Everywhere" />
> </configSections>
>
> <myCustomSection>
> <add key="LastErrorKey1" value="LastError" />
> <add key="LastErrorKey2" value="LastError" />
> <add key="LastErrorKey3" value="LastError" />
> <add key="LastErrorKey4" value="LastError" />
> ...
> </myCustomSection>
>
> Here is my MyHandler class
>
> namespace ComIT.Applications.Common.ConfigurationSections
> {
> public class MyHandler : ConfigurationSection
> {
> public MyHandler()
> {
> }
> }
> }
>
> I use this code to get my section.
> MyHandler config =
> (MyHandler)System.Configuration.ConfigurationManag er.GetSection("myCustomSection");
>
> This gives me following error:
> Unrecognized element 'add'. (c:\\Inetpub\\wwwroot\\MedTime\\web.config line
> 21).
>
> I searched from msdn. I found this topic
> http://msdn2.microsoft.com/en-us/library/2tw134k3.aspx. This does not fulfill
> my requirement either.
>
> I need a simple section like built-in <appSettings> og <connectionStrings>
> section. How can I do this?
>
> Cann't I define a property to my class MyHandler as
> System.Collections.Specialized.NameValueCollection that can hold all the
> values under my section like ConfigurationManager.AppSettings.
>
>
> Aftab Ahmad
> aa@com-it.dk




Nov 20 '05 #6
Hi Juan,

Thanks for your answer. I have already read all these links. I am trying to
find this out for last three days.

If you read my first email. I am using ConfigurationSection class but this
class supports only node without child nodes in the web.config. That means
that you can not populate children to this node. If you try to add “add” node
then you get following error.

"Unrecognized element 'add'. (c:\\Inetpub\\wwwroot\\MedTime\\web.config line
21.)"

When I use IConfigurationSectionHandler with
ConfigurationSettings.GetConfig() method then although it works but at the
same time it gives warning that “This method is obsolete. This method has
been replaced by ConfigurationManager.GetSection”.

Why have they made it so difficult? It was so easy in .NET 1.1. As I have
written before that I have also read this topic
http://msdn2.microsoft.com/en-us/library/2tw134k3.aspx where you can add
section group and populate also section child nodes but this is not good
solution. It works fine if you have only one section and child nodes but if
you have 100 sections of different types then your have create 100 classes to
read every section. Is it correct?

Is there any better solution in .NET 2.0? Thanks in advanced.

Aftab
"Juan T. Llibre" wrote:
Maybe I'm reading that page wrong, but just because it says that
"a configuration section handler had to implement the
System.Configuration.IConfigurationSectionHandler interface,
which is now deprecated" doesn't mean that page shows how to use
the System.Configuration.IConfigurationSectionHandler interface.

In fact, it doesn't.

The sample for 1.0/1.1 is at :
http://msdn2.microsoft.com/en-us/library/ms228056.aspx
and is a totally different coding method than what
http://msdn2.microsoft.com/en-us/library/ms228062.aspx
and the links on that page demonstrate.

See, in :
http://msdn2.microsoft.com/en-us/library/2tw134k3.aspx
the sections titled :

"To create a custom configuration section handler"
"To add a custom section handler to an ASP.NET configuration file"
"To programmatically access your custom configuration data"

Those do *not* use "IConfigurationSectionHandler"; they use
"System.Configuration.ConfigurationSection", a 2.0 class.

See this reference:
http://msdn2.microsoft.com/en-us/lib...onsection.aspx

There's example code on that page.


Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
ASPNETFAQ.COM : http://www.aspnetfaq.com/
Foros de ASP.NET en Español : http://asp.net.do/foros/
======================================
"Tabi" <Ta**@discussions.microsoft.com> wrote in message
news:AC**********************************@microsof t.com...
Hi Juan,

I have a code for .NET Framework versions 1.0 and 1.1 and I already have
read this topic, but they have also written a note in this topic.

In the .NET Framework versions 1.0 and 1.1, a configuration section handler
had to implement the System.Configuration.IConfigurationSectionHandler
interface, which is now deprecated. However, a code example exists in How to:
Create Custom Configuration Sections Using IConfigurationSectionHandler.

I want a real .NET Framework version 2.0 code. Have you any example for
that? Thanks in advanced.

Aftab Ahmad
"Juan T. Llibre" wrote:
This used to be real easy in 1.1 :

<configuration>
<configSections>
<sectionGroup name="system.web">
<section name="customSection"
type="System.Configuration.NameValueSectionHandler ,
System, Version=1.0.5000.0, Culture=neutral,
PublicKeyToken=b77a5c561934e089"/>
</configSections>
<customSection>
<add key="customSection Key 1" value="Value for customSection Key 1"/>
<add key="customSection Key 2" value="Value for customSection Key 2"/>
</customSection>
</configuration>

See if :
http://msdn2.microsoft.com/en-us/library/ms228062.aspx
sheds any more light on this for ASP.NET 2.0.

Juan T. Llibre, ASP.NET MVP
ASP.NET FAQ : http://asp.net.do/faq/
ASPNETFAQ.COM : http://www.aspnetfaq.com/
Foros de ASP.NET en Español : http://asp.net.do/foros/
======================================
<Ta**@discussions.microsoft.com> wrote in message
news:53**********************************@microsof t.com...
> Hi,
>
> I want to create a custom section in my web.config that can hold my custom
> values. I created a section in web.config as written below.
>
> <configSections>
> <section name="myCustomSection"
> type="ComIT.Applications.Common.ConfigurationSecti ons.MyHandler"
> allowLocation="true" allowDefinition="Everywhere" />
> </configSections>
>
> <myCustomSection>
> <add key="LastErrorKey1" value="LastError" />
> <add key="LastErrorKey2" value="LastError" />
> <add key="LastErrorKey3" value="LastError" />
> <add key="LastErrorKey4" value="LastError" />
> ...
> </myCustomSection>
>
> Here is my MyHandler class
>
> namespace ComIT.Applications.Common.ConfigurationSections
> {
> public class MyHandler : ConfigurationSection
> {
> public MyHandler()
> {
> }
> }
> }
>
> I use this code to get my section.
> MyHandler config =
> (MyHandler)System.Configuration.ConfigurationManag er.GetSection("myCustomSection");
>
> This gives me following error:
> Unrecognized element 'add'. (c:\\Inetpub\\wwwroot\\MedTime\\web.config line
> 21).
>
> I searched from msdn. I found this topic
> http://msdn2.microsoft.com/en-us/library/2tw134k3.aspx. This does not fulfill
> my requirement either.
>
> I need a simple section like built-in <appSettings> og <connectionStrings>
> section. How can I do this?
>
> Cann't I define a property to my class MyHandler as
> System.Collections.Specialized.NameValueCollection that can hold all the
> values under my section like ConfigurationManager.AppSettings.
>
>
> Aftab Ahmad
> aa@com-it.dk



Nov 20 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

reply views Thread by Sren Lund | last post: by
4 posts views Thread by Nick Gilbert | last post: by
1 post views Thread by npaulus | last post: by
2 posts views Thread by Laurent Bugnion | last post: by
6 posts views Thread by Jeff Hegedus | last post: by
7 posts views Thread by =?Utf-8?B?RG91Z2llIEJyb3du?= | last post: by
reply views Thread by =?Utf-8?B?UGhpbGlw?= | last post: by
reply views Thread by XIAOLAOHU | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.