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

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

最初,我在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)

但是我仍然在最后一行得到错误。

“ IConfiguration”不包含“ Get”的定义,最佳扩展方法重载“ SessionExtensions.Get(ISession,string)”需要类型为“ ISession”的接收器。

在这里也得到错误。

   public void ConfigureServices(IServiceCollection services)
    {
        // Add MVC services to the services container.
        services.AddMvc(); //error
    }
Run Code Online (Sandbox Code Playgroud)

我的project.json文件

    {
  "webroot": "wwwroot",
  "version": "1.0.0-*",
  "dependencies": {
    "Microsoft.AspNet.Diagnostics": "1.0.0-rc1-final",
    "Microsoft.AspNet.Mvc": "6.0.0-rc1-final",
    "Microsoft.AspNet.Mvc.TagHelpers": "6.0.0-rc1-final",
    "Microsoft.AspNet.Server.IIS": "1.0.0-beta7",
    "Microsoft.AspNet.Server.WebListener": "1.0.0-rc1-final",
    "Microsoft.AspNet.StaticFiles": "1.0.0-rc1-final",
    "Microsoft.AspNet.Tooling.Razor": "1.0.0-rc1-final",
    "Microsoft.Framework.Configuration.Json": "1.0.0-beta8",
    "Microsoft.Framework.Logging": "1.0.0-beta8",
    "Microsoft.Framework.Logging.Console": "1.0.0-beta8",
    "Microsoft.VisualStudio.Web.BrowserLink.Loader": "14.0.0-rc1-final",
    "EntityFramework": "6.1.3",
    "jqGridWebApi": "1.1.4",
    "EnyimMemcached": "2.13.0",
    "xunit": "2.2.0-beta1-build3239",
    "Moq": "4.2.1510.2205",
    "NLog": "2.1.0",
    "CryptSharpOfficial": "2.1.0",
    "System.Linq.Dynamic": "1.0.4"
  },
  "commands": {
    "web": "Microsoft.AspNet.Hosting --server Microsoft.AspNet.Server.WebListener --server.urls http://localhost:5001",
    "kestrel": "Microsoft.AspNet.Hosting --server Kestrel --server.urls http://localhost:5005"
  },
  "frameworks": {
    "dnx451": {
      "frameworkAssemblies": {
        "System.configuration": "4.0.0.0",
        "System.Data": "4.0.0.0"
      }
    }
  },
  "exclude": [
    "wwwroot",
    "node_modules",
    "bower_components"
  ],
  "publishExclude": [
    "node_modules",
    "bower_components",
    "**.xproj",
    "**.user",
    "**.vspscc"
  ],
  "scripts": {
    "prepublish": [ "npm install", "bower install", "gulp clean", "gulp min" ]
  },
  "configurations": {
    "Staging": { }
  }
}
Run Code Online (Sandbox Code Playgroud)

感谢帮助

Joh*_*hnB 8

安装NuGet软件包:

Microsoft.Extensions.Configuration.Binder

复制:将IConfigurationSection绑定到没有aspnetcore的复杂对象

NuGet软件包的屏幕截图

  • 请注意,您不需要为 Binder 使用 using 子句,只需为“using Microsoft.Extensions.Configuration;” (3认同)
  • 这应该是公认的答案,因为它直接回答了提问者的问题。 (2认同)
  • 这是解决方案,谢谢 (2认同)

Mic*_*osa 5

Get方法已不存在,您必须使用

Configuration.GetSection("key").Value.ToString();
Run Code Online (Sandbox Code Playgroud)


小智 1

对于 RC1,用于配置的 NuGet 包位于Microsoft.Extensions.Configuration.Json而不是Microsoft.Framework.Configuration.Json.

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