从appSettings.json获取价值?

时间:2017-03-23 10:19:09

标签: c# .net asp.net-core asp.net-core-mvc .net-core

我无法检索appsettings.json中设置的值,当我运行下面的代码时,出现错误System.NullReferenceException: 'Object reference not set to an instance of an object.'

我做错了什么?

public static IConfigurationRoot Configuration { get; } 
....
string facebookApiId = Configuration.GetValue<string>("Authentication:Facebook:AppId");

appSettings.json

"Authentication": {
  "Facebook": {
    "IsEnabled": "false",
    "AppId": "somevalue1",
    "AppSecret": "somevalue2"
  },
  "Google": {
    "IsEnabled": "false",
    "ClientId": "somevalue3",
    "ClientSecret": "somevalue4"
  }

Startup.cs

public Startup(IHostingEnvironment env)
{
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appsettings.json", optional: false, reloadOnChange: true)
        .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true)
        .AddEnvironmentVariables();
    Configuration = builder.Build();
}

public IConfigurationRoot Configuration { get; }

1 个答案:

答案 0 :(得分:6)

在您的代码中,您实际上有两(2)个Configuration属性,一个在Startup,这很好,因为它正在填充并存储在 instance 字段中,并且一个在未命名的控制器中,它是static,似乎从未实例化。

根据MSDN article about the configuration,为控制器提供选项的推荐方法是实现基本选项和配置对象逻辑,如下所示:

// option mapping classes
public class FacebookOptions
{
    // maybe string here
    public bool IsEnabled { get; set; }
    public string AppId { get; set; }
    public string AppSecret { get; set; }
}

public class GoogleOptions
{
    // maybe string here
    public bool IsEnabled { get; set; }
    public string ClientId { get; set; }
    public string ClientSecret { get; set; }
}

// load configuration
public Startup(IHostingEnvironment env)
{
    // Set up configuration sources.
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appSettings.json", optional: true, reloadOnChange: true);

    Configuration = builder.Build();
}

// map the configuration to object
public void ConfigureServices(IServiceCollection services)
{
    // Adds services required for using options.
    services.AddOptions();

    // Register the IConfiguration instance which options binds against.
    services.Configure<FacebookOptions>(Configuration.GetSection("Facebook"));
    services.Configure<GoogleOptions>(Configuration.GetSection("Google"));

    // Add framework services.
    services.AddMvc();
}

现在,您可以通过依赖注入轻松获取控制器上的选项:

public class GoogleController : Controller
{
    private readonly GoogleOptions _googleOptions;

    public GoogleController(IOptions<GoogleOptions> googleOptionsAccessor)
    {
        _googleOptions = googleOptionsAccessor.Value;
    }
}

如果您需要整个配置,可以添加一些包含所有选项的通用类,并使用同一篇文章中的object graph mapping

public class Authentication
{
    public FacebookOptions Google { get; set; }
    public GoogleOptions Google { get; set; }
}

// load configuration
public Startup(IHostingEnvironment env)
{
    // Set up configuration sources.
    var builder = new ConfigurationBuilder()
        .SetBasePath(env.ContentRootPath)
        .AddJsonFile("appSettings.json", optional: true, reloadOnChange: true);

    Configuration = builder.Build();

    var options = new Authentication();
    config.GetSection("Authentication").Bind(options);
}

编辑:确保你的类和配置部分的名称相同,因为这很重要,结果证明了这一点。