Web.config Appsettings section

Not getting the items in the AppSettings section

Page 1 of 1

2 Replies - 3182 Views - Last Post: 18 September 2009 - 07:27 PM Rate Topic: -----

#1 woodjom  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 29
  • View blog
  • Posts: 549
  • Joined: 08-May 08

Web.config Appsettings section

Post icon  Posted 18 September 2009 - 11:25 AM

I have been trying to get my mainpage to pull the information from the AppSettings section of my Web.Config file.

Im not sure if i am pulling the information too early in the page event process but here is what i have.

web.config section
<appSettings>
		<!--
			Use this section to setup default non-changing menu locations
			This defines the navigation menu of the site and as such should only
			  be changed at setup and left alone thereafter.
			
			Domain should be in the form of <name>.<tld>
			All the other keys should be the SubDomain name excluding the domain address
		-->
		<add key="Domain" value="silverlight3d.us"/>
		<add key="Home" value="www"/>
		<add key="Wiki" value="wiki"/>
		<add key="Forum" value="forum"/>
		<add key="Blog" value="blog"/>
	</appSettings>



Default.aspx code in question
	Private Function LoadMenuItems() As NameValueCollection
		Dim Items As New NameValueCollection
		Dim wConfig As System.Configuration.Configuration
		wConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration(Nothing)

		If wConfig.AppSettings.Settings.Count > 0 Then
			Dim cSettings As System.Configuration.KeyValueConfigurationCollection
			cSettings = wConfig.AppSettings.Settings
			Dim tDomain As String = cSettings.Item("Domain").Value

			For Each cElement As KeyValueConfigurationElement In cSettings
				Dim key As String = cElement.Key
				Dim value As String = Nothing

				If Not (key = "Domain") Then
					value = "http://" & cElement.Value & "." & tDomain
					Items.Add(key, value)
				End If
			Next
		End If
		Return Items
	End Function



i pretty much followed the rules supplied by MSDN and im getting a result count of 0 from the wConfig.AppSettings.Settings.Count property. Should i have this done on the PageLoad event? If so my next question is how the H*LL am i supposed to load a ASCX file that is dependent on the AppSettings section for basic navigation to properly render.

This function is being called for all intense and purposes in the PageInit event of the main page.

Is This A Good Question/Topic? 0
  • +

#4 woodjom  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 29
  • View blog
  • Posts: 549
  • Joined: 08-May 08

Re: Web.config Appsettings section

Posted 18 September 2009 - 01:40 PM

Well, figured it out. Seems that when you are referencing the local web.config file to the application, you need to explicitely tell the OpenWebConfiguration method to open the local.

for example:
wConfig = System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/web")



Otherwise it uses the servers web.config file. of which i rarely open and edit.

Wish i could thank myself. ;)

This post has been edited by woodjom: 18 September 2009 - 01:41 PM

Was This Post Helpful? 1

#5 rgfirefly24  Icon User is offline

  • D.I.C Lover
  • member icon


Reputation: 267
  • View blog
  • Posts: 1,470
  • Joined: 07-April 08

Re: Web.config Appsettings section

Posted 18 September 2009 - 07:27 PM

I'll thank you for this post. I just recently lost a web app because my coworker overwrote the production web.config which is what all Web apps on that server were referencing because of the exact same thing you were experiencing. Because of that my app took a big dump. Thanks for the information
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1