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

I have a web application deployed in IIS. This web application consumes a library that wants to access Web.config.

Example: Foo.dll is a web application deployed in IIS Foo.Utility.dll is consumed by Foo.dll

Foo.Utility namepsace has a code snippet that wants to access web.config from a Foo application and read the configuration 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:

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

Now my Assembly.GetCallingAssembly (). Location: 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 understand how to read web.config from where my application is deployed using IIS?

For more information, or if the question is not clear, give a comment below. Will be updated.

+6
source share
1 answer

You must use the ConfigurationManager from System.Configuration. First, you need to add a reference to the System.Configuration.dll assembly, and then use it as follows:

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

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

Link:

+3
source

Source: https://habr.com/ru/post/982536/


All Articles