c# - 为什么静态 Configuration.GetSection() 不可用?

标签 c# asp.net-core

在我的 ASP.NET Core 3.1 项目中,我正尝试通过依赖注入(inject)服务容器从“appsettings.json”读取配置选项,正如所解释的那样 by the docs或者也可以通过 this SO answer .

但是每当我需要将 Configuration.GetSection 添加到“Startup.cs”中的 ConfigureServices() 时,我都会收到错误消息:

CS0120  An object reference is required for the non-static field, method, or property 'Configuration.GetSection(string)'

enter image description here

static Configuration.GetSection是“.NET 平台扩展 3.1”的一部分。我需要安装依赖项/添加程序集吗?

我已经尝试安装 NuGet ConfigurationManager .

最佳答案

在 Program.cs 中

改变:

builder.Services.Configure(Configuration.GetSection("MailSettings"));

到:

builder.Services.Configure(builder.Configuration.GetSection("MailSettings"));

关于c# - 为什么静态 Configuration.GetSection() 不可用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64206226/

相关文章:

c# - 删除时的 EF5 Code First 级联

amazon-web-services - 使用 amazon cognito 的电子邮件验证链接

c# - 在 Razor Pages 中为 ASP.NET Core 2 Localizer 指定语言

c# - HttpWebRequest 对每个主机 2 个连接的限制是否适用于 HttpClient?

c# - Mono编译器(dmcs)如何引用DLL?

c# - 当我尝试在 MVVM WPF 中使用命令修改对象时,为什么在 View 模型中出现空错误?

c# - 类型 "Binding"的 DependencyProperty 未更新

asp.net - 无效操作异常 : No authentication handler is registered for the scheme Bearer.

asp.net-core - ASP.NET Core 中服务器端检测浏览器是否为 Internet Explorer

gruntjs - ASP .NET 5 - 将文件从节点模块复制到 wwwroot 的 grunt 任务