2016-07-21 66 views
7

由于在.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>' 

我错过了什么?

+0

你好,你有没有找到解决办法呢?我有完全相同的问题。 – AndyM

+0

是的,我做了,但我更改了appsettings.json内容以匹配我在其他地方看到的示例。 –

+0

查看已发布的答案 –

回答

3

尝试安装NuGet包Microsoft.Extensions.Configuration.Binder和使用it's Bind方法:

services.Configure<ConexionConfig>(x => Configuration.GetSection("Conexion").Bind(x)); 

您还必须安装选件包Microsoft.Extensions.Options并添加对它的支持,如果你想注入你的选项类别:

public void ConfigureServices(IServiceCollection services) 
{ 
    services.AddOptions(); 
    //.. 
} 

现在您可以在控制器和视图中注入IOptions<ConexionConfig>

+0

这样做对你来说是对的,但如果有人想用相同的方式配置它,很多教程都会说明问题,那么只需要添加正确的包。查看我的答案... –

0

我基于其他地方的例子。更改appsettings.json到这样的事情:

{ 
    "Logging": { 
    "IncludeScopes": false, 
    "LogLevel": { 
     "Default": "Debug", 
     "System": "Information", 
     "Microsoft": "Information" 
    } 
    }, 
    "Data": { 
    "DefaultConnection": { 
     "ConnectionString": "Data Source=myserver\\sql08;Initial Catalog=enterprises;User id=userAPP;Password=mypassword;" 
    } 
    } 
} 

ConexionConfig类改成这样:

public class ConexionConfig 
     { 

     public string ConnectionString { get; set; } 

    } 
} 

然后在Startup.cs

... 
public void ConfigureServices(IServiceCollection services) 
     { 
      // Add framework services. 
      services.AddMvc(); 
      services.Configure<ConexionConfig>(Configuration.GetSection("Data:DefaultConnection")); 
     } 
... 

重要的是要包括在此文件中using Microsoft.Extensions.Configuration

+0

我认为您的原始问题与此答案的唯一区别在于您沿途添加了正确的包裹。看到我的答案... –

8

首先,您需要将以下nuget包添加到您的ASP核心项目。

Microsoft.Extensions.Options.ConfigurationExtensions 

包中包含的扩展方法允许您按照您最初想要的方式配置强类型配置。

services.Configure<ConexionConfig>(Configuration.GetSection("Conexion")); 

或者,您可以使用粘合剂一样,直接在这个线程另一个答案建议没有重要以前的包,而是:

Microsoft.Extensions.Configuration.Binder 

这意味着,你必须明确地启用的选项在流水线上,并绑定该动作。那就是:

services.AddOptions(); 
services.Configure<ConexionConfig>(x => Configuration.GetSection("Conexion").Bind(x)); 
+0

这个答案很有帮助。请注意,使用** ASP.NET Core 2.0 **,不需要额外的NuGet。 –

相关问题