【发布时间】:2020-05-03 06:24:58
【问题描述】:
我搜索的主题
- ConfigurationManager.AppSettings count 0
- Reading settings from app.config or web.config in .NET
- ConfigurationManager.AppSettings is empty?
- WPF configurationmanager.appsettings collection is empty
我的应用程序是一个 .NET Core 3.1 应用程序,因此我通过 NuGet 将库 System.Configuration.ConfigurationManager 添加到我的项目中。我的根文件夹包含一个Web.Config,内容如下
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<system.web>
<compilation debug="true" />
<httpRuntime />
</system.web>
<appSettings>
<!-- Folder of LogParser job-configurations -->
<add key="JobFolder" value="App_Data/jobs"/>
<!-- Background task execution interval in seconds -->
<add key="Interval" value="5"/>
<!-- Message offset in seconds. Reads older messages to circumvent log4net timestamp bug -->
<add key="Offset" value="7200"/>
<!-- Caching duration for hash MemoryCache in seconds. Default is 604800 (7 days) -->
<add key="Caching" value="604800"/>
</appSettings>
</configuration>
但是,当我访问 ConfigurationManager.AppSettings[key] 时,它始终是空的。另外,Configurationmanager.AppSettings.AllKeys 为空,计数为 0,就好像没有被解析一样。
有什么想法吗?
【问题讨论】:
-
确定你不是想拥有一个 appSettings.json 文件?
-
我有一个,是的,但是docs.microsoft.com/de-de/dotnet/api/… 说我需要一个 .config 文件。我需要一个 appSettings.json 文件吗?
-
点网核心不使用
.config文件和配置管理器 -
@Nkosi 是的,我读到了,但是在将它添加为 NuGet 包时支持它(至少我是通过 Google 发现的)。还有其他获取内容的方法吗?
-
@TanveerBadar 你是对的。我说错了。它不再默认使用它。您现在必须显式添加/引用它才能使用它。