c# - 如何从 ASP.NET Core 中的 .json 文件读取 AppSettings 值

标签 c# asp.net-core .net-core configuration app-config

我已经在文件 appsettings/Config .json 中设置了我的 AppSettings 数据,如下所示:

{
  "AppSettings": {
        "token": "1234"
    }
}

我在网上搜索了如何从 .json 文件中读取 AppSettings 值,但没有得到任何有用的信息。

我试过:

var configuration = new Configuration();
var appSettings = configuration.Get("AppSettings"); // null
var token = configuration.Get("token"); // null

我知道使用 ASP.NET 4.0 可以做到这一点:

System.Configuration.ConfigurationManager.AppSettings["token"];

但我如何在 ASP.NET Core 中执行此操作?

最佳答案

这有一些曲折。我修改了这个答案,使其与 ASP.NET Core 2.0 保持同步(截至 2018 年 2 月 26 日)。

这主要取自the official documentation :

要在 ASP.NET 应用程序中使用设置,建议您只在应用程序的 Startup 类中实例化一个 Configuration。然后,使用选项模式访问各个设置。假设我们有一个如下所示的 appsettings.json 文件:

{
  "MyConfig": {
   "ApplicationName": "MyApp",
   "Version": "1.0.0"
   }

}

我们有一个代表配置的 POCO 对象:

public class MyConfig
{
    public string ApplicationName { get; set; }
    public int Version { get; set; }
}

现在我们在 Startup.cs 中构建配置:

public class Startup 
{
    public IConfigurationRoot Configuration { get; set; }

    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

        Configuration = builder.Build();
    }
}

请注意,appsettings.json 将在 .NET Core 2.0 中默认注册。如果需要,我们还可以为每个环境注册一个 appsettings.{Environment}.json 配置文件。

如果我们想将我们的配置注入(inject)我们的 Controller ,我们需要在运行时注册它。我们通过 Startup.ConfigureServices 这样做:

public void ConfigureServices(IServiceCollection services)
{
    services.AddMvc();

    // Add functionality to inject IOptions<T>
    services.AddOptions();

    // Add our Config object so it can be injected
    services.Configure<MyConfig>(Configuration.GetSection("MyConfig"));
}

然后我们像这样注入(inject)它:

public class HomeController : Controller
{
    private readonly IOptions<MyConfig> config;

    public HomeController(IOptions<MyConfig> config)
    {
        this.config = config;
    }

    // GET: /<controller>/
    public IActionResult Index() => View(config.Value);
}

完整的 Startup 类:

public class Startup 
{
    public IConfigurationRoot Configuration { get; set; }

    public Startup(IHostingEnvironment env)
    {
        var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);

        Configuration = builder.Build();
    }

    public void ConfigureServices(IServiceCollection services)
    {
        services.AddMvc();

        // Add functionality to inject IOptions<T>
        services.AddOptions();

        // Add our Config object so it can be injected
        services.Configure<MyConfig>(Configuration.GetSection("MyConfig"));
    }
}

关于c# - 如何从 ASP.NET Core 中的 .json 文件读取 AppSettings 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31453495/

相关文章:

c# - 通过代码打开机器/基地Web.Config(64位)

c# - 从数据库问题下载 ASP.NET MVC 3 文件

c# - 在 Heroku 上为只监听 https 的服务使用哪个环境变量

c# - 使用 hang-fire 和 ASP.NET Core 发送每日摘要电子邮件

c# - 如何在 Web API 的响应中附加文件

c# - 如何在 .NET Core 中创建动画 gif

c# - foreach 循环获取 JSON 值

c# - 即使项目生成,Visual Studio 也会显示错误

asp.net-core - 发布自包含应用程序 .NET Core 应用程序

configuration - 测试应该在 dotnet core 的 Debug 还是 Release 配置中运行