小编use*_*022的帖子

如何在 ASP.NET Core 6 控制器中访问 appsettings.json 中的值

我根据 Visual Studio 2022 中提供的默认模板创建了一个全新的 ASP.NET Core Web API 项目 (.NET 6)。之后,我在 appsettings.json 文件中添加了一个配置密钥controller并尝试在类中访问它,但不能。

我的appsettings.json文件如下:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*",
  "ProjectName" : "VeryFunny"
}

Run Code Online (Sandbox Code Playgroud)

控制器代码如下:

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> …
Run Code Online (Sandbox Code Playgroud)

c# asp.net-core-webapi .net-6.0

6
推荐指数
2
解决办法
2万
查看次数

标签 统计

.net-6.0 ×1

asp.net-core-webapi ×1

c# ×1