由于在.NET的核心没有ConfigurationManager中类,现在我必须设置在appsettings.json配置,而不是web.config中不能从JSON的AppSettings在.NET的核心项目文件中设置配置
根据this博客文章我有在那里设置我的配置,所以我这样做:
{
"Logging": {
"IncludeScopes": false,
"LogLevel": {
"Default": "Debug",
"System": "Information",
"Microsoft": "Information"
}
},
"Conexion": {
"name" : "empresas",
"connectionString": "Data Source=empresas;Initial Catalog=CATALMA; Integrated Security=True;",
"providerName": "System.Data.SqlClient"
}
}
我刚才说“Conexion”。
现在我在的ViewModels创建的文件夹下面的类:
public class ConexionConfig
{
public string name { get; set; }
public string connectionString { get; set; }
public string providerName { get; set; }
}
现在,在Startup.cs,在ConfigureServices方法,我必须要添加:
public void ConfigureServices(IServiceCollection services)
{
// Add framework services.
services.Configure<ConexionConfig>(Configuration.GetSection("Conexion"));
services.AddMvc();
}
但不幸的是,我收到以下错误:
Argument 2: cannot convert from
'Microsoft.Extensions.Configuration.IConfigurationSection' to
'System.Action<ConexionConfig>'
我错过了什么?
你好,你有没有找到解决办法呢?我有完全相同的问题。 – AndyM
是的,我做了,但我更改了appsettings.json内容以匹配我在其他地方看到的示例。 –
查看已发布的答案 –