Asp.net core 从appsettings.json到options的绑定部分

Asp.net core 从appsettings.json到options的绑定部分,asp.net-core,asp.net-core-3.1,appsettings,Asp.net Core,Asp.net Core 3.1,Appsettings,因此,我试图遵循Microsoft文档,但就我的一生而言,我似乎无法正确地将“AppSettings”部分作为选项添加到我的AppSettings对象中。配置.GetSection(“AppSettings”);对我来说是否返回空值 appsettings.Development.json { "AppSettings": { "Secret": "DEVELOPMENTTOKENTEST" }, "Ser

因此,我试图遵循Microsoft文档,但就我的一生而言,我似乎无法正确地将“AppSettings”部分作为选项添加到我的AppSettings对象中。配置.GetSection(“AppSettings”);对我来说是否返回空值

appsettings.Development.json

{
  "AppSettings": {
    "Secret": "DEVELOPMENTTOKENTEST"
  },
  "Serilog": {
    "MinimumLevel": {
      "Default": "Debug",
      "Override": {
        "Default": "Information",
        "Microsoft": "Warning",
        "Microsoft.Hosting.Lifetime": "Information"
      }
    },
    "Enrich": [ "FromLogContext", "WithMachineName" ],
    "Properties": {
      "Application": "Oasis.API"
    },
    "WriteTo": [
      {
        "Name": "File",
        "Args": {
          "path": "Logs/api_.log",
          "outputTemplate": "{Timestamp:o} [{Level:u3}] ({SourceContext}) {Message}{NewLine}{Exception}",
          "rollingInterval": "Day",
          "retainedFileCountLimit": 1
        }
      }
    ]
  },
  "AllowedHosts": "*",
  "ConnectionStrings": {
    "DefaultConnection": "Data Source=JS-PC;Initial Catalog=Oasis;Integrated Security=True"
  }
}
为我的应用程序设置部分初始化

public class AppSettings
{
        public string Secret { get; set; }
}
我试图阅读本节中的内容,并将其配置为在服务中使用依赖项注入

Startup.cs

public void ConfigureServices(IServiceCollection services)
{
            services.AddCors();
            services.AddControllers();

            // Configure API secret
            var test = Configuration.GetSection("AppSettings"); // null... oh?
            services.Configure<AppSettings>(con => Configuration.GetSection("AppSettings").Bind(con));

            // Swagger generation
            services.AddSwaggerGen();

            // Database and Repositories
            ConfigureRepositories(services);
            
            // Register Services
            ConfigureServiceClasses(services);
}
public void配置服务(IServiceCollection服务)
{
services.AddCors();
services.AddControllers();
//配置API机密
var test=Configuration.GetSection(“AppSettings”);//null…噢?

服务。配置

您不需要设置。它的实现将由DI系统注入。您可以使用以下命令获取
机密:

using Microsoft.Extensions.Configuration;
//....
private readonly IConfiguration _mySettings;
public HomeController(IConfiguration mySettings)
    {
        _mySettings = mySettings;
    }
public IActionResult Index()
    {
        var result = _mySettings.GetValue<string>("AppSettings:Secret");
        //...
    }
使用Microsoft.Extensions.Configuration;
//....
私有只读IConfiguration\u mySettings;
公共家庭控制器(IConfiguration mySettings)
{
_mySettings=mySettings;
}
公共IActionResult索引()
{
var result=_mySettings.GetValue(“AppSettings:Secret”);
//...
}
测试结果:

顺便说一下,如果您想注入它,可以使用以下代码

 //...
  var settingsSection =Configuration.GetSection("AppSettings");
  services.Configure<AppSettings>(settingsSection);
/。。。
var settingsSection=Configuration.GetSection(“AppSettings”);
服务。配置


参考此项。

显然,这可以从AppSettings.Development.json
var test=Configuration.GetSection(“AppSettings”).Get();
创建具有正确秘密属性的AppSettings对象,但是我不知道通过服务注入它的方法。Configure();您好@Jdog,有关于这个案例的更新吗?如果您有任何进展或错误,您可以第一时间告诉我,这样我就可以为您解决问题。@Yinqiu我想我终于明白了为什么我的
\u appSettings=appSettings.Value;
会变为null,这是因为我试图在libr类中的一个服务中删除IOPSary没有引用我的API项目。谢谢。高兴可以帮助您:)当我按照您在回答的第二部分中演示的注入方式进行注入时,当我执行与您对
\u settings=settingsAccessor.Value;
执行相同的操作时,我的值为空。请确保您的代码与我的代码相同。请检查它。
 //...
  var settingsSection =Configuration.GetSection("AppSettings");
  services.Configure<AppSettings>(settingsSection);
 private readonly IConfiguration _mySettings;
 private readonly AppSettings _settings;

 public HomeController(IOptions<AppSettings> settingsAccessor, IConfiguration mySettings)
    {
        _mySettings = mySettings;
         _settings = settingsAccessor.Value;
    }
  public IActionResult Index()
    {
        var vm = new AppSettings
        {
          Secret= _settings.Secret
        };
        var result = _mySettings.GetValue<string>("AppSettings:Secret");
    }