C# ConfigurationManager.AppSettings无法找到设置
以下web.conig文件位于项目内部(在本例中为单元测试项目): 此外,System.Web.Configuration.WebConfigurationManager.AppSettings.AllKeys只列出了一个键,在Web.config文件中根本找不到它。事实上,在整个解决方案中搜索这些键什么也找不到C# ConfigurationManager.AppSettings无法找到设置,c#,web-config,C#,Web Config,以下web.conig文件位于项目内部(在本例中为单元测试项目): 此外,System.Web.Configuration.WebConfigurationManager.AppSettings.AllKeys只列出了一个键,在Web.config文件中根本找不到它。事实上,在整个解决方案中搜索这些键什么也找不到 如何更正WebConfiguration Manager未返回正确密钥的行为?我看到您引用了app.config和web.config 如果您试图使用web.config设置,您可能应
如何更正WebConfiguration Manager未返回正确密钥的行为?我看到您引用了app.config和web.config 如果您试图使用web.config设置,您可能应该使用 比如:
若它在测试项目内部,那个么它可能不应该被称为web.config。将其重命名为app.config。我正试图找出ASP.net项目的一个问题,因为从web.config读取时遇到了完全相同的问题,所以理想情况下,我会尽可能地复制它。我按照您的建议进行了测试(并编辑了我的原始帖子以进行调整,因为我认为这是必需的),但端口和主机的计算结果仍然为空。
...
<appSettings>
...
<add key ="SmtpPort" value="00"/>
<add key="SmtpHost" value="site.site.com"/>
<add key=""/>
</appSettings>
...
[TestMethod]
public void VerifyAppSettingsAreRead()
{
string port = System.Web.Configuration.WebConfigurationManager.AppSettings["SmtpPort"];
string host = System.Web.Configuration.WebConfigurationManager.AppSettings["SmtpHost"];
Assert.IsTrue(!String.IsNullOrEmpty(port) && !String.IsNullOrEmpty(host));
}
[TestMethod]
public void VerifyAppSettingsAreRead()
{
string port = System.Web.Configuration.WebConfigurationManager.AppSettings["SmtpPort"];
string host = System.Web.Configuration.WebConfigurationManager.AppSettings["SmtpHost"];
Assert.IsTrue(!String.IsNullOrEmpty(port) && !String.IsNullOrEmpty(host));
}