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
The web. config is a file that is read by IIS and the ASP.NET Core Module to configure an app hosted with IIS.
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.
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:
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