Paul Fleming
Paul Fleming

Reputation: 24526

web.config in subdirectory in mvc 4

This question seems to have been asked quite a bit but none actually solve the problem for me. I have an ASP.NET MVC4 application with an area. Inside this area I have a web.config with overridden appsettings. These settings don't seem to be picked up when attempting to read them in a controller within the area.

-MyApp
    -Areas
        -MyArea
            -Controllers
                -MyController
            -Web.config
    -Web.config

In MyApp\Web.config I have:

<appSettings>
    <add key="DefaultDisplayMode" value="Configuration" />
</appSettings>

In MyApp\Areas\MyArea\Web.config I have:

<appSettings>
    <add key="DefaultDisplayMode" value="Review" />
    <add key="SubSetting" value="Testing" />
</appSettings>

In MyController if I query the app settings, I see:

DefaultDisplayMode: Configuration

I expect to see:

DefaultDisplayMode: Review
SubSetting: Testing

I've tried with both:

System.Configuration.ConfigurationManager.AppSettings["..."]
System.Web.Configuration.WebConfigurationManager.AppSettings["..."]

I even tried with the following (over the top) structure with no luck:

-MyApp
    -Areas
        -MyArea
            -Controllers
                -MyController
                -Web.config
            -Web.config
    -Web.config

Does anybody know why my subdirectory appSettings aren't working?

Upvotes: 4

Views: 1767

Answers (1)

Knaģis
Knaģis

Reputation: 21485

IIS will load the web.config based on the URL, not based on the code that your routed request executes.

You can create an empty folder structure that mimics the required route and add the config file there. If that does not work, you can use <location> tag in the main configuration file - but the path in the location tag also has to match the URL and not the code.

Upvotes: 2

Related Questions