我正在开发ASP.NET MVC项目,并决定将一些功能提取到单独的类库项目中(在同一个解决方案中)。
我从主项目(让我们称之为A项目)的Web.config
中移动了一些元素到类库项目(让我们称之为B项目)的App.config
中的<appSettings></appSettings>
。 我添加了System.Configuration.dll的引用以访问B项目中的ConfigurationManager。
B项目的示例App.config文件如下。
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<appSettings>
<add key="Key1" value="Value1"/>
<add key="Key2" value="Value2"/>
</appSettings>
</configuration>
在项目B中,我按照以下方式访问这些设置。
string key1 = ConfigurationManager.AppSettings["Key1"];
string key2 = ConfigurationManager.AppSettings["Key2"];
我注意到由ConfigurationManager
返回的key1
和key2
的值为null
。
调试时,我发现ConfigurationManager
从项目A中的原始Web.config
中提取了值(在<appsettings></appsettings>
部分中我未移动到项目B中的其他条目)!
这对我来说没有任何意义。有人可以告诉我我做错了什么,以便我可以访问App.config中的设置吗?
谢谢。