【问题标题】:Configuration.GetSection is returning null in .net core (3.1)Configuration.GetSection 在 .net 核心 (3.1) 中返回 null
【发布时间】:2020-04-23 08:46:21
【问题描述】:

我正在尝试使用“Configuation.GetSection”获取信息 appSettings.json 文件。但不知何故我无法得到这个。

这里是代码

appSettings.Development.json:

{
    "test": {
    "a": "sdfs"
}

startup.cs

var test = Configuration.GetSection("test");

我需要运行什么来从 appsettings.json 文件中获取信息。

请帮忙。提前致谢

【问题讨论】:

  • 这不是 .net core 3.1 的一部分。我可以看到文件正在根属性下加载
  • 是的,我很确定。
  • 更新了屏幕截图,我可以在其中看到这些文件中的 json 文件和数据。
  • 这能回答你的问题吗? Configuration.GetSection always returns null

标签: .net asp.net-core configuration


【解决方案1】:

我想我找到了上述问题的答案。我不确定我是否正确。

我想从“apSettings.json”文件中获取设置并创建一个对象,然后将它与 DI 一起使用到我的控制器中。

为此我现在使用这种方式。

在 ConfigureServices 方法中:

public void ConfigureServices(IServiceCollection services)
    {
        AppSettings _settings = new AppSettings();
        services.AddControllersWithViews();
        Configuration.GetSection("AppSettings").Bind(_settings);// bind is necessary
        services.AddSingleton(_settings);
    }

(注意:AppSettings 是自定义类,将具有 appSettings.json 中提到的属性)

选项 2: 这是从“appsettings.json”获取信息的第二个选项。

services.AddSingleton<AppSettings>(Configuration.GetSection("AppSettings").Get<AppSettings>();

【讨论】:

    【解决方案2】:

    Configuration.GetSection() 方法返回具有指定子节键的配置子节,它返回指定的 ConfigurationSection 对象。您可以使用以下方式获取数据:

    1.从配置对象中获取section。在这里面,我们得到了另一个包含值的部分。

    var test = Configuration.GetSection("Test").GetSection("a").Value;
    

    2.直接从配置对象中获取字符串类型值。我们用“:”分隔嵌套部分。

    string dbConn2 = Configuration.GetValue<string>("Test:a");
    

    参考:

    https://www.c-sharpcorner.com/article/reading-values-from-appsettings-json-in-asp-net-core/

    【讨论】:

      【解决方案3】:

      我也遇到了类似的问题,其中 IConfiguration 为“null”,因为我放置在 appsettings.json 文件中的键/值对没有被读取。我做了一些实验,发现这种方法可行(在 .net core 3.1 中):

      在应用程序的 Program.cs 文件中,添加您的配置文件配置:

      var host = new HostBuilder()
                       .UseContentRoot(Directory.GetCurrentDirectory())
                       
                       .ConfigureWebHostDefaults(webBuilder =>
                       {
                           webBuilder.ConfigureAppConfiguration(x =>
                           {
                               x.SetBasePath(Environment.CurrentDirectory);
                               x.AddJsonFile("appsettings.json", optional: false, reloadOnChange: true);
                               x.AddEnvironmentVariables();
                               x.Build();
                           });
      
                           webBuilder.UseKestrel(config =>
                           {
                               
                           })
                           .UseIIS()
                           .UseStartup<Startup>();
                       }).Build();
                  host.Run();
      

      【讨论】:

      • 太棒了。这对我有用。
      【解决方案4】:

      确保配置正确

      appsettings.json

      {
        ...
        "Test": {
          "Key": "Value" 
        }
      }
      

      Startup.cs

      public class Startup
      {
          public Startup(IConfiguration configuration)
          {
              Configuration = configuration;
          }      
          public IConfiguration Configuration { get; }
      
      
          ...
      
          string v = Configuration["Test:Key:Value"];
      
          ...
      }
      

      【讨论】:

      • 嗨 Mateech,我已经正确配置了。目前,如果我只给出“测试”作为键,它就不会到来。但是如果我给“测试:a”。它给了我价值。奇怪的事情。
      • 嗨@DeepakSingh,你可以试试这样:..GetSection("test").GetValue&lt;string&gt;("a")
      • 是的,我可以这样做,但它会给我一个值,我想将设置绑定到对象中。然后使用 DI 使用它。
      • @DeepakSingh 也许这会有所帮助:stackoverflow.com/questions/46017593/…
      猜你喜欢
      • 2022-08-18
      • 2022-07-08
      • 2019-05-27
      • 1970-01-01
      • 1970-01-01
      • 2018-02-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多