在我的 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)'
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/