从appsettings.json读取数据无效
我试图从appsetings.json文件中读取一些数据,如下所示:从appsettings.json读取数据无效,json,asp.net-core,Json,Asp.net Core,我试图从appsetings.json文件中读取一些数据,如下所示: public void ConfigureServices(IServiceCollection services) { services.AddOptions(); //sercies.Configure<Models.AppDate>(Configuration); services.Configure<Models.AppData>(Conf
public void ConfigureServices(IServiceCollection services)
{
services.AddOptions();
//sercies.Configure<Models.AppDate>(Configuration);
services.Configure<Models.AppData>(Configuration.GetSection("AppData"));
//It does not works
string option = Configuration.Get<Models.AppData>().AnotherOption;
//It works
string anotherOption = Configuration["AppData:AnotherOption"];
// Add framework services.
services.AddMvc();
}
在appsettings.json中:
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Warning"
}
},
"AppData": {
"Jwt": {
"Audience": "http://localhost:5000",
"Issuer": "http://localhost:5000"
},
"AnotherOption": "Not yet"
}
}
当我调试时,选项var为null。?我如何实现这一点?。Ty我不太清楚上面的代码为什么不起作用。 但我知道其他的方法
string option=Configuration.GetSection(“AppData”).Get().AnotherOption代码>
或
string option=ConfigurationBinder.Get(Configuration.GetSection(“AppData”))。另一个选项;
我不太清楚上面的代码为什么不起作用。
但我知道其他的方法
string option=Configuration.GetSection(“AppData”).Get().AnotherOption代码>
或
string option=ConfigurationBinder.Get(Configuration.GetSection(“AppData”))。另一个选项;
下面的方法可以从appsettings.json访问数据
public void ConfigureServices(IServiceCollection services)
{
var config = Configuration.GetSection("Application").Get<Application>();
}
//Model class
public class Application
{
public string ServiceUrl { get; set; }
}
public void配置服务(IServiceCollection服务)
{
var config=Configuration.GetSection(“应用程序”).Get();
}
//模范班
公共类应用程序
{
公共字符串ServiceUrl{get;set;}
}
下面的方法可以从appsettings.json访问数据
public void ConfigureServices(IServiceCollection services)
{
var config = Configuration.GetSection("Application").Get<Application>();
}
//Model class
public class Application
{
public string ServiceUrl { get; set; }
}
public void配置服务(IServiceCollection服务)
{
var config=Configuration.GetSection(“应用程序”).Get();
}
//模范班
公共类应用程序
{
公共字符串ServiceUrl{get;set;}
}
如果我试图从控制器中的AppData类中获取值,我可以获取它们,但它们在ConfigureServices方法中不可见。欢迎提供任何建议。如果我尝试从控制器中的AppData类获取值,我可以获取它们,但它们在ConfigureServices方法中不可见。欢迎提供任何建议。
public IConfiguration Configuration { get; }
public void ConfigureServices(IServiceCollection services)
{
var config = Configuration.GetSection("Application").Get<Application>();
}
//Model class
public class Application
{
public string ServiceUrl { get; set; }
}