Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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()
like image 289
Portekoi Avatar asked Oct 07 '13 09:10

Portekoi


People also ask

Can we use multiple Web config in site?

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.

Where are web config files located?

The Web. Config file is used to configure Oracle Web Application functionality. This file is typically installed in the c:\Inetput\wwwroot\WebApp directory.

Where is ASP.NET config?

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.

Where is Web config in .NET Core?

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.

Where is the web config file located in MVC?

The Machine. config file is located in the %SystemRoot%\Microsoft.NET\Framework\%VersionNumber%\CONFIG\ folder. The default settings that are contained in the Machine.


2 Answers

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
like image 150
Robert Avatar answered Sep 23 '22 15:09

Robert


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?

like image 27
Alex Avatar answered Sep 20 '22 15:09

Alex