Mustafa Shabib
Mustafa Shabib

Reputation: 740

Multiple Web.config files in Area using ASP.NET MVC2

I have a question on something that I thought would be a simple. I have an Area in my website and I'd like to create a Web.config file specific to this area but, no matter where I put this config file, the keys that I've set up in my appSettings are all returning "null" when I access them, so I'm not sure what I'm supposed to do. Here's my website structure:

Site
    [usual site stuff like Views, Models, Controllers] 
    Web.config
    Areas
        MyArea
            Web.config
            [usual site stuff like Views, Models, Controllers] 

my web.config on the main site looks something like...

<?xml version="1.0"?>
<configuration>
<appSettings>
    <add key="MyMainKey" value="MyTestValue"/>
</appSettings>
<!--all of the standard asp.net mvc config stuff -->
</configuration>

My areas config looks something like

<?xml version="1.0"?>
<configuration>
<appSettings>
    <add key="MyAreaConfigValue" value="MyAreaTestValue"/>
</appSettings>
<!--all of the standard asp.net mvc config stuff -->
</configuration>

In a controller action that exists in my Areas/MyArea/Controllers folder I'm trying to access it like so:

string my_area_config_val = System.Configuration.ConfigurationManager.AppSettings["MyAreaTestValue"];

But my_area_config_val is null.

In a controller action in my main site (not in my Area) I can do

string my_main_config_val = System.Configuration.ConfigurationManager.AppSettings["MyTestValue"];

and my_main_config_val is equal to "MyTestVal", as expected.

Thanks. Mustafa

Upvotes: 1

Views: 3120

Answers (1)

Justin Schwartzenberger
Justin Schwartzenberger

Reputation: 2684

The Web.config file goes in the Area/Views dir. I have not found any concrete facts, but from what I have seen in doing some research is that the <appSettings> from Web.config files within Areas will never get used. Below is one site that has a brief statement on it:

Upvotes: 2

Related Questions