Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read web.config from library consumed by the webapplicaion deployed using IIS

I have a web application deployed on IIS. This web application is consuming a library which wants to access the Web.config.

Example : Foo.dll is the web application deployed on IIS Foo.Utility.dll is consumed by Foo.dll

There is a piece of code in Foo.Utility namepsace which wants to access the web.config from Foo application and read the config values

Configuration config = WebConfigurationManager.OpenWebConfiguration(null);
string cacheDir = config.AppSettings.Settings["abc"].Value;

Currently config.FilePath = C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\web.config

Changed my code to :

Configuration config = WebConfigurationManager.OpenWebConfiguration(Assembly.GetCallingAssembly().Location);
string cacheDir = config.AppSettings.Settings["abc"].Value;

Now my Assembly.GetCallingAssembly().Location is : C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\root\62f5c902\849205ff\assembly\dl3\c28d4647\10e128d3_7449d001\foo.dll

Can someone help me to understand how to read the web.config from the place where my application is deployed using IIS?

For more information or if the question is not clear then comment below. Will update it

like image 436
StackOverflowVeryHelpful Avatar asked Feb 16 '15 00:02

StackOverflowVeryHelpful


People also ask

Does IIS use Web config?

The web. config is a file that is read by IIS and the ASP.NET Core Module to configure an app hosted with IIS.

Where is the IIS Web config file located?

The configuration files for IIS 7 and later are located in your %WinDir%\System32\Inetsrv\Config folder, and the primary configuration files are: ApplicationHost. config - This configuration file stores the settings for all your Web sites and applications. Administration.


1 Answers

You have to use ConfigurationManager from System.Configuration. First, you'll have to add a reference to System.Configuration.dll assembly, and then use it like this:

using System.Configuration;
...
string valueOfAbc = ConfigurationManager.AppSettings["abc"];

ConfigurationManager will read the config file from the application's host. In your case, the web.config file.

Reference:

  1. ConfigurationManager Class
  2. Application Configuration Files
like image 192
Augusto Barreto Avatar answered Nov 08 '22 02:11

Augusto Barreto