Artem Vertiy
Artem Vertiy

Reputation: 1118

MVC 3 WebConfigurationManager.AppSettings["key"] returns null

May be it's a stupid but seems mvc has a bug with web.configs.

I put in Web.config (also I've tried to put keys in web.debug.config) several appsettings keys But

ConfigurationManager.AppSettings["key"]; 

as well

WebConfigurationManager.AppSettings["key"];

returns null.

I looked via debugger in AppSettings.AllKeys - it sees the right keys but returns the null.

What is the problem with AppSettings in mvc?

Upvotes: 1

Views: 6217

Answers (4)

Artem Vertiy
Artem Vertiy

Reputation: 1118

oh, I've found the mistake. The problem was in key names, keys were created using copy-paste and it is not noticeable when some keys have the space before closing ", for example key="HPPRequestUrl "

Upvotes: 2

Rikard
Rikard

Reputation: 3869

In your webconfig you should have something like this:

<configuration>
 <appSettings>
  <add key="key" value="x" /> 
 </appSettings>
</configuration>

And then you can retrieve your value by wrtiting:

WebConfigurationManager.AppSettings["key"];

Upvotes: 0

Ivan Nikitin
Ivan Nikitin

Reputation: 4125

I looked via debugger in AppSettings.AllKeys - it sees the right keys but returns the null.

Probably you have a cyrillic "C" character or a typo in the key name. Try to copy the key name from the AllKeys property using debugger.

Upvotes: 0

Kimpo
Kimpo

Reputation: 5836

Here is someone with similiar problems. maybe it helps appSettings and ConfigurationManager.AppSettings issue

Upvotes: 0

Related Questions