从使用 IIS 部署的 Web 应用程序使用的库中读取 web.config
Read web.config from library consumed by the webapplicaion deployed using IIS
我在 IIS 上部署了一个 Web 应用程序。此 Web 应用程序正在使用一个想要访问 Web.config.
的库
示例:
Foo.dll是IIS上部署的web应用
Foo.Utility.dll 被 Foo.dll
消耗
Foo.Utility namepsace 中有一段代码想要从 Foo 应用程序访问 web.config 并读取配置值
Configuration config = WebConfigurationManager.OpenWebConfiguration(null);
string cacheDir = config.AppSettings.Settings["abc"].Value;
目前config.FilePath = C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\web.config
将我的代码更改为:
Configuration config = WebConfigurationManager.OpenWebConfiguration(Assembly.GetCallingAssembly().Location);
string cacheDir = config.AppSettings.Settings["abc"].Value;
现在我的 Assembly.GetCallingAssembly()。位置是:C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\rootf5c9029205ff\assembly\dl3\c28d4647e128d3_7449d001\foo。 dll
有人可以帮助我了解如何从使用 IIS 部署我的应用程序的地方读取 web.config 吗?
有关更多信息或如果问题不清楚,请在下面发表评论。会更新的
您必须使用 System.Configuration 中的 ConfigurationManager。首先,您必须添加对 System.Configuration.dll 程序集的引用,然后像这样使用它:
using System.Configuration;
...
string valueOfAbc = ConfigurationManager.AppSettings["abc"];
ConfigurationManager 将从应用程序的主机读取配置文件。在你的例子中,web.config 文件。
参考:
我在 IIS 上部署了一个 Web 应用程序。此 Web 应用程序正在使用一个想要访问 Web.config.
的库示例: Foo.dll是IIS上部署的web应用 Foo.Utility.dll 被 Foo.dll
消耗Foo.Utility namepsace 中有一段代码想要从 Foo 应用程序访问 web.config 并读取配置值
Configuration config = WebConfigurationManager.OpenWebConfiguration(null);
string cacheDir = config.AppSettings.Settings["abc"].Value;
目前config.FilePath = C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Config\web.config
将我的代码更改为:
Configuration config = WebConfigurationManager.OpenWebConfiguration(Assembly.GetCallingAssembly().Location);
string cacheDir = config.AppSettings.Settings["abc"].Value;
现在我的 Assembly.GetCallingAssembly()。位置是:C:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\rootf5c9029205ff\assembly\dl3\c28d4647e128d3_7449d001\foo。 dll
有人可以帮助我了解如何从使用 IIS 部署我的应用程序的地方读取 web.config 吗?
有关更多信息或如果问题不清楚,请在下面发表评论。会更新的
您必须使用 System.Configuration 中的 ConfigurationManager。首先,您必须添加对 System.Configuration.dll 程序集的引用,然后像这样使用它:
using System.Configuration;
...
string valueOfAbc = ConfigurationManager.AppSettings["abc"];
ConfigurationManager 将从应用程序的主机读取配置文件。在你的例子中,web.config 文件。
参考: