Portekoi
Portekoi

Reputation: 1147

Sub Web.config in Area

I'm working on a huge MVC 4 project. I'm using Area and it's great.

But, I want, for each Areas, use a custom Web.config.

For example, this is my structure :

Root
Web.config
|
----Areas
--------MyArea_1
--------MyArea_2
----Web.config

In my root Web.config, i've all the connections. In my Sub Web.config, i want to add "appSettings" that concern only Areas.

In my sub Web.config, for example :

<appSettings>
    <add key="MyArea_1_Param1" value="1"/>
    <add key="MyArea_1_Param2" value="2"/>

    <add key="MyArea_2_Param1" value="1"/>
</appSettings>

For the moment, i've put all of my parameters in the root Web.config.

How can I make a link between my different Web.config?

------UPDATE--------

I've try to put this code in my Sub Web.config :

<?xml version="1.0" encoding="utf-8"?>
<location path="MyArea_1">
    <appSettings>
        <add key="MyArea_1_Param1" value="1"/>
        <add key="MyArea_1_Param2" value="2"/>
    </appSettings>
</location>

A I put this file, here :

Root
Web.config
|
----Areas
--------MyArea_1
--------MyArea_2
----Web.config <- Here

I already try here to :

Root
Web.config
|
----Areas
--------MyArea_1
------------Web.config
--------MyArea_2

But i'm still getting a "NullReferenceException" when I try to read the key value "MyArea_1_Param1"

My code for read the key value :

ConfigurationManager.AppSettings["MyArea_1_Param1"].ToString()

Upvotes: 5

Views: 9586

Answers (2)

Robert
Robert

Reputation: 3302

You can use:

<location path="myarea">
  <appsetttings>
    <add key="MyArea_1_Param1" value="1"/>
  </appsettings>
</location>

and put these web.configs under area directories

To access your web.config in given area, you can use code like this:

System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/Areas/MyArea1/Views/Web.config").AppSettings.Settings["MyArea1_Param1"].Value

Upvotes: 5

Alex
Alex

Reputation: 1687

Web.config file for each area is placed in the Views folder of this area by default. You can use it to add custom settings that will be visible only in that particular area.

Update

Have you seen this answer?

Upvotes: 1

Related Questions