[英]How to get value from appsettings.json in Program class in asp.net core console application
[英]How to access a value from appsettings.json in ASP.NET Core 6 controller
我从 Visual Studio 2022 中提供的默认模板创建了一个全新的 ASP.NET Core Web API 项目 (.NET 6)。之后,我在 appsettings.json 文件中添加了一个配置键,并尝试在881290954395811821995811821995818182188 controller
访问它但不能。
我的appsettings.json文件如下:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ProjectName" : "VeryFunny"
}
controller代码如下:
public class WeatherForecastController : ControllerBase
{
private static readonly string[] Summaries = new[]
{
"Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
};
private readonly IConfiguration _configuration;
public WeatherForecastController(IConfiguration configuration)
{
_configuration = configuration;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = _configuration["ProjectName"] // <-- I added this
})
.ToArray();
}
}
我在上面的方法中收到null
而不是VeryFunny
字符串,我在其中添加了箭头注释。 我假设在 .NET 6 中合并Startup.cs
class 和Program.cs
class 后,现在IConfiguration
很容易获得,但看来我错了。
更新:
这是您在 Visual Studio 2022 中创建新的 ASP.NET Core Web API targeting .NET 6时获得的基本项目。当我创建一个目标为 .NET 5 的新项目时,我没有任何问题,因为在Startup.cs
和Porgram.cs
是单独的类,并且IConfiguration
的一个实例已经注入到Startup
class 中,而在 .NET 中不是这种情况 6. 有一个现有的 SO 问题问同样的问题,但该问题及其答案集中在 .NET 之前的 .NET 实现6,例如.NET Core 2.x,.NET 5等。我的是.NET 6具体。
您将需要使用.GetValue<type>
,因此请尝试
Summary = _configuration.GetValue<string>("ProjectName");
您还可以查看详细帖子https://stackoverflow.com/a/58432834/3559462 (显示如何获取价值)
https://qawithexperts.com/article/asp-net/read-values-from-appsettingsjson-in-net-core/467 (显示来自 appsettings.json 的 Model 的绑定)
更新:
我创建了新的 .NET Core 6 API 项目并使用了这个代码
private readonly IConfiguration _configuration;
private readonly ILogger<WeatherForecastController> _logger;
public WeatherForecastController(ILogger<WeatherForecastController> logger, IConfiguration configuratio)
{
_logger = logger;
_configuration = configuratio;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = _configuration.GetValue<string>("ProjectName")
})
.ToArray();
它对我有用,这是 GIF 图像
这些选项可能会有所帮助:
appsettings.json
文件没有复制到建筑物上。 右键单击appsettings.json
文件和 select 属性。 然后Copy to Output Directory
应该等于Copy if newer
或Copy always
。appsettings.json
、 appsettings.Development.json
、 appsettings.Production.json
。 确保您的密钥( ProjectName
)在所有这些中我想出了如何使用构造函数中的 IConfiguration 接口从 controller 访问 appsettings.json。
下面是我的代码示例:
public class HomeController : Controller
{
private readonly IConfiguration _config;
private string ConnectionString;
public HomeController(IConfiguration configuration)
{
_config = configuration;
ConnectionString = _config["Configurations:ConnectionString"];
Console.WriteLine(ConnectionString);
}
连接字符串(在本例中是 json 文件中的参数)显示成功。 我的应用程序设置:json 代码:
"Configurations": {
"ConnectionString": "MyDBConnectionStringExample;"
}
如果是这种情况,我真的建议您需要一个单独的 class 来处理所有数据库通信,否则它工作得很好......希望它能帮助别人。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.