appSettings and ConfigurationManager.AppSettings issue


Question

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/"/>
    <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?

1
17
4/3/2009 12:07:01 AM

Have you tried using the WebConfigurationManager:

return System.Web.Configuration.WebConfigurationManager.AppSettings["APIKey"];

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

29
7/21/2012 3:20:26 AM

Licensed under: CC-BY-SA with attribution
Not affiliated with: Stack Overflow
Icon