我正在使用.NET 6創建 API。我有基于環境的appsettings.json檔案。
我也創建了 apt 組態檔launchSettings.json。
我正在嘗試使用Options模式來讀取其中一個控制器內的鍵/值。
NULL我每次都得到這些值。
當我除錯 Program.cs 檔案時,我可以通過下面的代碼行看到正確的值。
builder.Configuration.GetSection(key: "Config").Get<Config>();
請幫忙。使用的代碼如下:
程式.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>();
組態檔:
public class Config
{
public string Environment { get; set; }
public string Type { get; set; }
}
ProductsController.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);
}
appsettings.json:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"Config": {
"Environment": "local",
"Type": "local Server"
}
}
appsettings.Development.json:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"Config": {
"Environment": "Development",
"Type": "Development Server"
}
}
appsettings.Staging.json:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"Config": {
"Environment": "Staging",
"Type": "Staging Server"
}
}
啟動設定.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"
}
}
}
輸出:

uj5u.com熱心網友回復:
我認為您可能需要使用configuration.GetSectionin Services.Configurewhich 表示從appsettings.json部分注冊Config物件。Config
builder.Services.Configure<Config>(configuration.GetSection("Config"));
uj5u.com熱心網友回復:
我認為您的問題在于:
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>();
您目前正在獲取一個部分Config,然后呼叫Get<>它,但從未將其應用于任何東西(注入)。看起來最后一行會這樣做,但是這個函式沒有引數來提供注入的東西。
你可以:
builder.Services.Configure<Config>(builder.Configuration.GetSection("Config"));
uj5u.com熱心網友回復:
services.AddOptions<Config>()
.Bind(configuration.GetSection("Config"))
.ValidateDataAnnotations(); // optionally validate annotations
嘗試使用 AddOptions,這應該會系結。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/465321.html
