我想學習ASP.NET 5.我在Mac OS X上使用它。此時,我有一個config.json文件,如下所示:ASP.NET 5(vNext) - 配置
config.json
{
"AppSettings": {
"Environment":"dev"
},
"DbSettings": {
"AppConnectionString": "..."
},
"EmailSettings": {
"EmailApiKey": "..."
},
}
我試圖弄清楚如何將這些設置加載到Startup.cs的配置文件。目前,我有一個看起來像這樣的文件:
Configuration.cs
public class AppConfiguration
{
public AppSettings AppSettings { get; set; }
public DbSettings DbSettings { get; set; }
public EmailSettings EmailSettings { get; set; }
}
public class AppSettings
{
public string Environment { get; set; }
}
public class DbSettings
{
public string AppConnectionString { get; set; }
}
public class EmailSettings
{
public string MandrillApiKey { get; set; }
}
然後,在Startup.cs,我有以下幾點:
public IConfiguration Configuration { get; set; }
public Startup(IHostingEnvironment environment)
{
var configuration = new Configuration().AddJsonFile("config.json");
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
services.Configure<AppConfiguration>(Configuration);
}
然而,這種方法不起作用。基本上,它像它不知道如何在.json和Config類之間進行映射。我該怎麼做呢?
我真的很想繼續使用DI方法,所以我可以更有效地測試我的應用程序。
謝謝
@KamranShahid這並沒有解決這個問題。 –