001*_*001 5 .net c# asp.net-core-mvc .net-core asp.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");
Run Code Online (Sandbox Code Playgroud)
appSettings.json
"Authentication": {
"Facebook": {
"IsEnabled": "false",
"AppId": "somevalue1",
"AppSecret": "somevalue2"
},
"Google": {
"IsEnabled": "false",
"ClientId": "somevalue3",
"ClientSecret": "somevalue4"
}
Run Code Online (Sandbox Code Playgroud)
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; }
Run Code Online (Sandbox Code Playgroud)
在你的代码中你实际上有两(2)个Configuration
属性,一个属性,Startup
这是好的,因为它被填充并存储在实例字段中,一个在未命名的控制器中,这static
似乎永远不会被实例化.
根据MSDN关于配置的文章,为控制器提供选项的推荐方法是实现基本选项和配置对象逻辑,如下所示:
// 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();
}
Run Code Online (Sandbox Code Playgroud)
现在,您可以通过依赖注入轻松获取控制器上的选项:
public class GoogleController : Controller
{
private readonly GoogleOptions _googleOptions;
public GoogleController(IOptions<GoogleOptions> googleOptionsAccessor)
{
_googleOptions = googleOptionsAccessor.Value;
}
}
Run Code Online (Sandbox Code Playgroud)
如果需要完整配置,可以添加一些包含所有选项的泛型类,并使用同一篇文章中的对象图映射:
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);
}
Run Code Online (Sandbox Code Playgroud)
编辑:确保您的类和配置部分的名称相同,因为这很重要,结果证明.
归档时间: |
|
查看次数: |
12182 次 |
最近记录: |