读取 .NET 6 API 中的 appsettings.{env}.json 对象 - 值为 NULL

Anu*_*rag 5 c# api json .net-6.0

我用来.NET 6创建 API。我有基于环境的appsettings.json文件。

launchSettings.json我也创建了 apt 配置文件。

我正在尝试使用Options模式来读取其中一个控制器内的键/值。

NULL我每次都会得到这些值。

当我调试 Program.cs 文件时,我可以通过下面的代码行看到正确的值。

builder.Configuration.GetSection(key: "Config").Get<Config>();
Run Code Online (Sandbox Code Playgroud)

请帮忙。使用的代码如下:

程序.cs:

var builder = WebApplication.CreateBuilder(args);
var configuration = builder.Configuration;

var env = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT");

configuration
    .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
    .AddJsonFile($"appsettings.{env}.json", true, true);

builder.Configuration.GetSection(key: "Config").Get<Config>();

builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddScoped<IProductRepository, ProductRepository>();
builder.Services.AddSingleton<Config>();
Run Code Online (Sandbox Code Playgroud)

配置.cs:

public class Config
    {
        public string Environment { get; set; }
        public string Type { get; set; }
    }
Run Code Online (Sandbox Code Playgroud)

产品控制器.cs

    [Route("api/[controller]")]
    [ApiController]
    public class ProductsController : ControllerBase
    {
        private readonly IProductRepository _repository;
        private readonly Config _config;

        public ProductsController(IProductRepository repository,
            IOptions<Config> config)
        {
            _repository = repository;
            _config = config.Value;
        }

        [HttpGet]
        [Route("/configs")]
        public IActionResult GetConfigs()
        {
            var model = new { type = _config.Type, env = _config.Environment };
            return Ok(model);
        }
Run Code Online (Sandbox Code Playgroud)

应用程序设置.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "Config": {
    "Environment": "local",
    "Type": "local Server"
  }
}
Run Code Online (Sandbox Code Playgroud)

appsettings.Development.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "Config": {
    "Environment": "Development",
    "Type": "Development Server"
  }
}
Run Code Online (Sandbox Code Playgroud)

appsettings.Staging.json:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "Config": {
    "Environment": "Staging",
    "Type": "Staging Server"
  }
}
Run Code Online (Sandbox Code Playgroud)

启动设置.json:

 "profiles": {
    "API - Development": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "launchUrl": "swagger",
      "applicationUrl": "https://localhost:7082;http://localhost:5082",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Development"
      }
    },
    "API - Staging": {
      "commandName": "Project",
      "dotnetRunMessages": true,
      "launchBrowser": true,
      "applicationUrl": "https://localhost:5001;http://localhost:5000",
      "environmentVariables": {
        "ASPNETCORE_ENVIRONMENT": "Staging"
      }
    }
}
Run Code Online (Sandbox Code Playgroud)

输出: 在此输入图像描述

D-S*_*hih 6

我认为您可能需要使用configuration.GetSection其中Services.Configure方法从appsettings.json部分注册Config对象。Config

builder.Services.Configure<Config>(configuration.GetSection("Config"));
Run Code Online (Sandbox Code Playgroud)