如何从 ASP.NET Core 中的 .json 文件读取 AppSettings 值 [英] How to read AppSettings values from a .json file in ASP.NET Core

查看:34
本文介绍了如何从 ASP.NET Core 中的 .json 文件读取 AppSettings 值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have set up my AppSettings data in file appsettings/Config .json like this:

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

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

I have searched online on how to read AppSettings values from .json file, but I could not get anything useful.

我试过了:

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

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

I know with ASP.NET 4.0 you can do this:

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

但是我如何在 ASP.NET Core 中做到这一点?

But how do I do this in ASP.NET Core?

推荐答案

这有一些曲折.我已修改此答案以与 ASP.NET Core 2.0 保持同步(截至 2018 年 2 月 26 日).

This has had a few twists and turns. I've modified this answer to be up to date with ASP.NET Core 2.0 (as of 26/02/2018).

这主要取自官方文档:

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

To work with settings in your ASP.NET application, it is recommended that you only instantiate a Configuration in your application’s Startup class. Then, use the Options pattern to access individual settings. Let's say we have an appsettings.json file that looks like this:

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

}

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

And we have a POCO object representing the configuration:

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

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

Now we build the configuration in 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 配置文件.

Note that appsettings.json will be registered by default in .NET Core 2.0. We can also register an appsettings.{Environment}.json config file per environment if needed.

如果我们想将我们的配置注入我们的控制器,我们需要在运行时注册它.我们通过 Startup.ConfigureServices 来实现:

If we want to inject our configuration to our controllers, we'll need to register it with the runtime. We do so via 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"));
}

我们像这样注入它:

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"));
    }
}

这篇关于如何从 ASP.NET Core 中的 .json 文件读取 AppSettings 值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆