【发布时间】:2017-12-15 06:29:21
【问题描述】:
我试图从Startup.cs 中的注入配置中调用GetSection。
值为null,而indexer 到具体部分值返回non-null 值。在我看来,GetSection 方法背后有一个错误,或者我错了?
appsettings.json:
{ “我的配置”:{ “配置A”:“值A”, “配置B”:“值B”}}
Program.cs:
public static void Main(string[] args)
{
var host = BuildWebHost(args);
host.Run();
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseStartup<Startup>()
.Build();
Startup.cs:
public class Startup
{
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public IConfiguration Configuration { get; }
// This method gets called by the runtime. Use this method to add services to the container.
public void ConfigureServices(IServiceCollection services)
{
services.AddMvc();
}
// This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
public void Configure(IApplicationBuilder app, IHostingEnvironment env)
{
var mySection = this.Configuration.GetSection("MyConfig");
var myVal = this.Configuration["MyConfig:ConfigA"];
【问题讨论】:
标签: c# json configuration .net-core