{"id":511,"date":"2003-09-26T00:39:11","date_gmt":"2003-09-26T04:39:11","guid":{"rendered":"http:\/\/wordpress.cephas.net\/?p=511"},"modified":"2003-09-26T00:39:11","modified_gmt":"2003-09-26T04:39:11","slug":"extending-webconfig-in-aspnet","status":"publish","type":"post","link":"https:\/\/cephas.net\/blog\/2003\/09\/26\/extending-webconfig-in-aspnet\/","title":{"rendered":"Extending web.config in ASP.NET"},"content":{"rendered":"<p>Last March I <a href=\"http:\/\/cephas.net\/blog\/archives\/000340.html#000340\">wrote<\/a> about how handy it is that ASP.NET enables you to add your own custom settings to the web.config file using syntax like this:<\/p>\n<p>&lt;appSettings&gt;<br \/>\n&lt;add key=&#8221;connString&#8221; value=&#8221;user id=sa;password=mypassword;&#8221; \/&gt;<br \/>\n&lt;\/appSettings&gt;<\/p>\n<p>This is nice, but can quickly get unwieldy if you have alot application settings to maintain.  Back in March I failed to mentioned (because I didn&#8217;t know about it) that you can extend your web.config even further by creating your own custom configuration sections.  You can read more about the <a href=\"http:\/\/msdn.microsoft.com\/library\/default.asp?url=\/library\/en-us\/cpgenref\/html\/gngrfConfigurationSectionsSchema.asp?frame=true\">ConfigurationSettingsSchema<\/a> on msdn, but here&#8217;s a quick example.  First you define the custom application settings:<\/p>\n<p>&lt;configSections&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&lt;sectionGroup name=&#8221;myapplication&#8221;&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;section name=&#8221;types&#8221; type=&#8221;System.Configuration.NameValueSectionHandler&#8221;\/&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;section name=&#8221;security&#8221; type=&#8221;System.Configuration.NameValueSectionHandler&#8221;\/&gt;<br \/>\n&nbsp;&nbsp;&nbsp; &lt;\/sectionGroup&gt;<br \/>\n&lt;\/configSections&gt;<\/p>\n<p>and then you add the actual settings:<\/p>\n<p>&lt;myapplication&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&lt;types&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;add key=&#8221;section&#8221; value=&#8221;com.ignitionlabs.Section&#8221; \/&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;add key=&#8221;template&#8221; value=&#8221;com.ignitionlabs.Template&#8221; \/&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&lt;\/types&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&lt;security&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;add key=&#8221;\/controlpanel\/&#8221; value=&#8221;Systems Administrators,Project Managers&#8221; \/&gt;<br \/>\n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&lt;add key=&#8221;\/members\/&#8221; value=&#8221;members&#8221; \/&gt;&nbsp;&nbsp;&nbsp;<br \/>\n&nbsp;&nbsp;&nbsp;&lt;\/security&gt;<br \/>\n&lt;\/myapplication&gt;<\/p>\n<p>Finally, you can use the exact same XPATH syntax that you use when retrieving other application settings.  In the above example, there are multiple &#8216;type&#8217; elements, so I&#8217;d first get a NameValueCollection object:<\/p>\n<p>NameValueCollection config = (NameValueCollection)System.Configuration.ConfigurationSettings.GetConfig(&#8220;myapplication\/types&#8221;);<\/p>\n<p>and then retrieve the value of the &#8220;section&#8221; key using the name:<\/p>\n<p>string section = config[&#8220;section&#8221;];<\/p>\n<p>Enjoy!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last March I wrote about how handy it is that ASP.NET enables you to add your own custom settings to the web.config file using syntax like this: &lt;appSettings&gt; &lt;add key=&#8221;connString&#8221; value=&#8221;user id=sa;password=mypassword;&#8221; \/&gt; &lt;\/appSettings&gt; This is nice, but can quickly get unwieldy if you have alot application settings to maintain. Back in March I failed &hellip; <a href=\"https:\/\/cephas.net\/blog\/2003\/09\/26\/extending-webconfig-in-aspnet\/\" class=\"more-link\">Continue reading <span class=\"screen-reader-text\">Extending web.config in ASP.NET<\/span> <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":[],"categories":[17],"tags":[],"_links":{"self":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/511"}],"collection":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/comments?post=511"}],"version-history":[{"count":0,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/posts\/511\/revisions"}],"wp:attachment":[{"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/media?parent=511"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/categories?post=511"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/cephas.net\/blog\/wp-json\/wp\/v2\/tags?post=511"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}