相关疑难解决方法(0)

如何在 .net Core 应用程序中使用 IConfiguration 绑定多级配置对象?

我正在尝试绑定到应该由 appsettings.json 文件填充的自定义配置对象。

我的 appsettings 看起来有点像:

{
  "Logging": {
    "IncludeScopes": true,
    "LogLevel": {
      "Default": "Debug",
      "System": "Information",
      "Microsoft": "Information"
    }
  },
  "Settings": {
    "Foo": {
      "Interval": 30,
      "Count": 10
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

设置类如下所示:

public class Settings
{
  public Foo myFoo {get;set;}
}

public class Foo
{
  public int Interval {get;set;}
  public int Count {get;set;}

  public Foo()
  {}

  // This is used for unit testing. Should be irrelevant to this question, but included here for completeness' sake.
  public Foo(int interval, int …
Run Code Online (Sandbox Code Playgroud)

c# json .net-core asp.net-core

5
推荐指数
1
解决办法
9256
查看次数

“ IConfiguration”不包含“ Get”的定义

最初,我在1.0.0-rc1-beta6中有代码。

 public static IConfiguration Configuration { get; set; }

 public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
    {
        // Setup configuration sources.
        var builder = new ConfigurationBuilder(appEnv.ApplicationBasePath)
            .AddJsonFile("config.json")
            .AddEnvironmentVariables();
        Configuration = builder.Build();
        var test = Configuration.Get("ASPNET_ENV");
    }
Run Code Online (Sandbox Code Playgroud)

现在我想使用1.0.0-rc1-update,代码是:

 public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
    {
        // Setup configuration sources.
        var builder = new ConfigurationBuilder()
            .SetBasePath(appEnv.ApplicationBasePath)
            .AddJsonFile("config.json")
            .AddEnvironmentVariables();
        Configuration = builder.Build();
        var test = Configuration.Get("ASPNET_ENV");
    }
Run Code Online (Sandbox Code Playgroud)

“ ASP.NET_ENV”来自launchSettings.json文件。

"profiles": {
"IIS Express": {
  "commandName": "IISExpress",
  "launchBrowser": true,
  "environmentVariables": {
    "ASPNET_ENV": "Development"
  },
  "sdkVersion": "dnx-clr-win-x64.1.0.0-beta6"
}, …
Run Code Online (Sandbox Code Playgroud)

asp.net-core asp.net-core-1.0

2
推荐指数
3
解决办法
8324
查看次数

标签 统计

asp.net-core ×2

.net-core ×1

asp.net-core-1.0 ×1

c# ×1

json ×1