views:

2222

answers:

7

I have searched the site, and while I found some very useful information, I couldn't figure out what is going on with my code. I have the following web.config:

<?xml version="1.0"?>
<configuration>
  <system.web>
  </system.web>

  <system.webServer>
  </system.webServer>

  <appSettings>
    <add key="APIKey" value="23e24c73feed7ca0f6afd876575842de"/>
    <add key="Secret" value="################################"/>
    <add key="Callback" value="http://localhost:55994/"/&gt;
    <add key="Suffix" value="My3Words"/>
  </appSettings>
</configuration>

I have snipped out the stuff in system.web and system.webServer, but it's the default settings generated in an ASP.NET MVC app.

I am trying to access the keys in the section (this is a simple Facebook application using FB Connect).

In my code, I have the following line:

return ConfigurationManager.AppSettings["APIKey"];

and it is returning a null. I can't quite figure out what is going on. I have the requisite:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Web.Mvc.Ajax;
using System.Configuration;

at the top of my .cs file. I have a very strong suspicion the error exists post-keyboard (i.e. in my brain), but I can't solve this one. Any ideas?

A: 

Have you tried:

return ConfigurationManager.AppSettings.Get("APIKey");
Michael Kniskern
Tried it...it didn't work
Brandon Watson
+1  A: 

Check to make sure the Build Action of the web.config file is "None". I have seen this problem if the build action is "Embedded Resource".

Nebakanezer
It was set to "content" but I set to "None" with no effect.
Brandon Watson
+1  A: 

Does your machine.config have the requisite AppSettings section. It should look something like (version numbers would be different):

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

Are you sure you're using the correct web.config? What does ConfigurationManager.AppSettings.Settings look like?

Mark Brackett
A: 

Hi,

I bet u r using the right syntax to access them when you write: ConfigurationManager.AppSettings["Key1"];

Try this format for the web config

<?xml version="1.0"?>
<configuration xmlns="http://schemas.microsoft.com/.NetConfiguration/v2.0"&gt;
<appSettings>
 <add key="Key1" value="Val1"/>
 <add key="Key2" value="Val2"/>
</appSettings>
<connectionStrings>
 ......
 ......
</connectionStrings>
<system.web>
 <sessionState mode="InProc" timeout="20"/>
 <authorization>
  ......
 </authorization>

 <pages>
  <namespaces>
   <add namespace="System.Data"/>
   <add namespace="System.Data.SqlClient"/>
   <add namespace="System.IO"/>
  </namespaces>
 </pages>
 <customErrors mode="Off"/>
 <compilation debug="true"/></system.web>
</configuration>
MSIL
+2  A: 

Have you tried using the WebConfigurationManager:

return WebConfigurationManager.AppSettings["APIKey"];

This is the preferred option for using config files in a web app - it handles things like nested config files, etc.

Zhaph - Ben Duguid
This did not work even though I had multiple web.config files. I have moved the <appSettings> to the other file and both methods work fine for getting the key. Thanks for the answer...I am an idiot.
Brandon Watson
Ok, so which folder was this web.config in? Was it at least under (in the folder tree) the location the code was running from? You're not an idiot ;)
Zhaph - Ben Duguid
I had this same issue as @Brandon. This answer was the solution for me. My project was a WCF Service Library and when I hosted it in IIS and moved the configuration to Web.config from App.config.
kenny
A: 

I hate getting faded by stupidity. The question about the machine.config led me to the right answer. I have no idea how it happened, but I had 2 web.config files in the project and I was editing one while the system was clearly reading the other. Doht!

Brandon Watson
+2  A: 

Visual Studio creates 2 web.config files for MVC. 1 at the root, and the other in the Views folder.

robertz