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()
Yes you can have two web. config files in application. There are situations where your application is divided in to modules and for every module you need separate configuration. For example if you have a application which has two modules lets say accounts and sales.
The Web. Config file is used to configure Oracle Web Application functionality. This file is typically installed in the c:\Inetput\wwwroot\WebApp directory.
config file is located in the %SystemRoot%\Microsoft.NET\Framework\%VersionNumber%\CONFIG\ directory. In the Machine. config file, locate the configuration setting you want to override in your Web.
config file location. In order to set up the ASP.NET Core Module correctly, the web. config file must be present at the content root path (typically the app base path) of the deployed app.
The Machine. config file is located in the %SystemRoot%\Microsoft.NET\Framework\%VersionNumber%\CONFIG\ folder. The default settings that are contained in the Machine.
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
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?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With