繁体   English   中英

从 asp.net 核心中的 appsettings.json 读取配置时出现空错误

[英]Getting null error when reading configuration from appsettings.json in asp.net core

我创建了一个简单的 ASP.NET Core 6 MVC 应用程序,我只是从appsettings.json文件中读取值。

在我的appsettings中,我创建了两个部分SectionASectionB

{
  "SectionA": {
    "SectionASubItem1": "test value",
    "SectionASubItem2": "test value2"
  },
  "SectionB": {
    "SectionBSubItem1": "test value",
    "SectionBSubItem2": "test value2"
  }
}

我还创建了一个类来将此值与类属性映射:

这是我的MyConfiguration类的样子:

public class MyConfiguration
{
    public SectionA SectionA { get; set; }
    public SectionB SectionB { get; set; }
}

public class SectionA
{
    public string SectionASubItem1 { get; set; }
    public string SectionASubItem2 { get; set; }
}

public class SectionB
{
    public string SectionBSubItem1 { get; set; }
    public string SectionBSubItem2 { get; set; }
}

program.cs类中,我创建了一个IConfigurationRoot变量类型,我在其中存储了 JSON 文件属性:

var myConfiguration = new ConfigurationBuilder()
    .SetBasePath(Directory.GetCurrentDirectory())
    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
    .Build();

并像这样映射部分:

builder.Services.Configure<SectionA>(myConfiguration.GetSection("SectionA"));
builder.Services.Configure<SectionB>(myConfiguration.GetSection("SectionB"));

问题是当我在控制器中读取sectionAsectionB时,我得到了空值。 因此,我没有从 JSON 文件中获取值。

public readonly MyConfiguration configuration;

public WeatherForecastController()
{
    configuration = new MyConfiguration();
}

[HttpGet(Name = "GetWeatherForecast")]
public IActionResult Get()
{
    // Here I am getting null
    var sectionA = configuration.SectionA;

    if (sectionA == null)
        return BadRequest();

    return Ok();
}

你可以这样做

在 appsettings.json

 "test": {
        "MyConfiguration": {
          "SectionA": {
            "SectionASubItem1": "test value",
            "SectionASubItem2": "test value2"
          },
          "SectionB": {
            "SectionBSubItem1": "test value",
            "SectionBSubItem2": "test value2"
          }
        }
      }

在 programm.cs 中

builder.Services.Configure<MyConfiguration>(myConfiguration.GetSection("test:MyConfiguration"));

接着

private readonly MyConfiguration _myConfiguration;

public WeatherForecastController(IOptions<MyConfiguration> options)
{
    _myConfiguration = options.Value;
}

var sectionA = _myConfiguration.SectionA; bla bla

最好使用 IOptions、IoptionsSnapshot 或 IOptionsMonitor。

IOptionsMonitor 与 IOptionsSnapshot 之间的区别

像这样设置你的appsetting.json

"MyConfiguration": {
    "SectionA": {
      "SectionASubItem1": "test value AAAAAA",
      "SectionASubItem2": "test value2 AAAA"
    },
    "SectionB": {
      "SectionBSubItem1": "test value BBBBBB",
      "SectionBSubItem2": "test value2 BBBBBB"
    }
  }

然后在Program.cs中配置如下:

builder.Services.Configure<SectionB>(myConfiguration.GetSection("MyConfiguration"));

最重要的一点是不要在你的构造函数中使用new MyConfiguration() ,你需要使用dependency injection来设置值。

private readonly IOptions<MyConfiguration> _configuration;

public WeatherForecastController(IOptions<MyConfiguration> configuration)
{
    _configuration= configuration;
}

然后可以在_configuration中获取值:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM