我无法检索在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; }
最佳答案
在您的代码中,您实际上有2个(两个)Configuration
属性,一个在Startup
中,这很好,因为它正在填充并存储在实例字段中,而另一个在未命名的 Controller 中,它是static
,似乎从未实例化。
根据MSDN article about the configuration,向 Controller 提供选项的推荐方法是实现基本的选项和配置对象逻辑,如下所示:
// 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();
}
现在,您可以通过依赖注入(inject)轻松地在 Controller 上获取选项:
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);
}
编辑:确保您的类和配置节的名称相同,因为事实很重要。
关于c# - 从appSettings.json获得值(value)?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/42973173/