Brandon Watson
Brandon Watson

Reputation: 1775

appSettings and ConfigurationManager.AppSettings issue

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?

Upvotes: 18

Views: 70007

Answers (8)

mattnedrich
mattnedrich

Reputation: 8072

Are you sure that you're using the correct Web.Config file? You probably want to use the one at the application root, not the one in the Views directory.

ASP.NET MVC and two Web.config files

Upvotes: 4

robertz
robertz

Reputation: 778

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

Upvotes: 9

zaheer ahmad
zaheer ahmad

Reputation: 294

i have two project in my solution first i add app.config file in class library project which all instances is call from console application i added these entries in config file in class lib project

<appSettings> 
<add key="userName" value="user2" /> 
<add key="emilsLimit" value="50" /> 
</appSettings>

it was throwing null exception when i get these in a class in class library project but when i delete app.config from class Library project and added in Console project it works.Cheers

Note: Class lib project reference is added in console

Upvotes: 0

Zhaph - Ben Duguid
Zhaph - Ben Duguid

Reputation: 26976

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.

Upvotes: 30

MSIL
MSIL

Reputation: 2761

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

Upvotes: 0

Mark Brackett
Mark Brackett

Reputation: 85685

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?

Upvotes: 1

Mark Sherretta
Mark Sherretta

Reputation: 10230

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".

Upvotes: 1

Michael Kniskern
Michael Kniskern

Reputation: 25290

Have you tried:

return ConfigurationManager.AppSettings.Get("APIKey");

Upvotes: -1

Related Questions