asp.net - 无法从 App.Config 检索 key

标签 asp.net

在我的 WAP 项目中,我引用了另一个 C# 项目(非 Web)。在那个 C# 项目中,我有一个 app.config 并将其重命名为 [projectName].config

然后我尝试从我的 [project].config 中从我的 C# 项目中的一些代码中获取一个 key ,但找不到它:

return ConfigurationManager.AppSettings["somekey"]

所以我想知道为什么它不能读取我的 app.config。我认为 ConfigurationManager 能够从 Web 项目的 web.config 和我引用的项目中的 [projectName].config (app.config) 文件中读取 key ?

最佳答案

ConfigurationManager 将读取 the configuration of the application's host 。在您的情况下,Web 应用程序是应用程序的主机 - 因此,调用 ConfigurationManager 将从您的 web.config 读取 - 即使您从引用的 dll 调用 ConfigurationManager。

如果您的 Web 应用程序引用了一个 dll,那么该 dll 应该从 web.config 中获取其配置。这是应用程序的最佳选择。如果 dll 被多种类型的应用程序引用,则 config 中的设置可能需要不同。

关于asp.net - 无法从 App.Config 检索 key ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2165413/

相关文章:

ASP.NET Core 2,IIS AppPool 崩溃并停止

c# - 使用 JavaScript 将参数传递给代码隐藏函数

c# - 在 ASP.NET 中 5 秒后重置表单和页面状态

c# - 在 asp.net 进程中运行多个并行任务的正确方法是什么?

c# - SSAS ADOMD 连接强制关闭

c# - Control.Visible 在控件生命周期的什么时候停止呈现?

c# - ASP.NET MVC5 自定义 AuthorizeAttribute 未被正确调用

c# - asp :dropdownlist doesn't save users selected value c# asp. 净aspx

c# - 任务完成后更新 ASP.NET 标签

c# - 使用自动映射器时无法从 'System.Collections.Generic.IEnumerable<x>' 转换为 'x'