简体   繁体   English

为什么 Visual Studio 告诉我 AddJsonFile() 方法没有定义?

[英]Why does Visual Studio tell me that the AddJsonFile() method is not defined?

I'm developing an ASP.NET 5 WebAPI project using VS Ultimate 2015 Preview.我正在使用 VS Ultimate 2015 Preview 开发一个 ASP.NET 5 WebAPI 项目。 I'm trying to configure the app in this way (line numbers are just guides):我正在尝试以这种方式配置应用程序(行号只是指南):

1 using Microsoft.Framework.ConfigurationModel;
2
3 public IConfiguration Configuration { get; private set; }
4 
5 public Startup()
6 {
7     Configuration = new Configuration()
8         .AddJsonFile("config.json")
9         .AddEnvironmentVariables();
10 }

Line 8 gives me an error: 'Configuration' does not contain a definition for 'AddJsonFile'...第 8 行给了我一个错误:“配置”不包含“AddJsonFile”的定义...

What is wrong?怎么了?

You need to include the Microsoft.Extensions.Configuration.Json NuGet package if you want to call the .AddJsonFile() method.如果要调用.AddJsonFile()方法,则需要包含Microsoft.Extensions.Configuration.Json NuGet 包。

See: https://github.com/aspnet/Configuration/tree/dev/src/Microsoft.Framework.ConfigurationModel.Json请参阅: https : //github.com/aspnet/Configuration/tree/dev/src/Microsoft.Framework.ConfigurationModel.Json

For further reading, here's a nice tutorial: ASP.NET vNext Moving Parts: IConfiguration .如需进一步阅读,这里有一个不错的教程: ASP.NET vNext 移动部件:IConfiguration

I know this is a bit old but I just ran into this issue when attempting to build my first Asp.net core 1.0 blank project.我知道这有点旧,但我在尝试构建我的第一个 Asp.net core 1.0 空白项目时遇到了这个问题。 In order to use the AddJsonFile method you must add a reference to Microsoft.Extensions.Configuration.Json to your project via Nuget .为了使用AddJsonFile方法,您必须通过Nuget向您的项目添加对Microsoft.Extensions.Configuration.Json的引用。

To install the reference run the below command in the package manager console:要安装参考,请在包管理器控制台中运行以下命令:

PM> Install-Package Microsoft.Extensions.Configuration.Json

In case anyone else has been having trouble with this, Microsoft have made breaking changes to this part of the framework on 16 August 2015. You must import the right versions of the dependencies and switch across to the new way of building up configuration.如果其他人遇到此问题,Microsoft 已于 2015 年 8 月 16 日对框架的这一部分进行了重大更改。您必须导入正确版本的依赖项并切换到构建配置的新方法。

My config includes:我的配置包括:

{
  "webroot": "wwwroot",
  "version": "1.0.0-*",

  "dependencies": {
    "Microsoft.Framework.Runtime": "1.0.0-*",
    "Microsoft.AspNet.Server.IIS": "1.0.0-beta7",
    "Microsoft.AspNet.Diagnostics": "1.0.0-beta7",
    "Microsoft.AspNet.Mvc": "6.0.0-beta7",
    "Microsoft.Framework.Configuration": "1.0.0-beta7",
    "Microsoft.Framework.Configuration.Json": "1.0.0-*"
  },
...
}

This code, inspired by this question might go some way to helping you:受此问题启发的此代码可能对您有所帮助:

using System;
using Microsoft.AspNet.Builder;
using Microsoft.Framework.DependencyInjection;
using Messenger.Services;
using Microsoft.Framework.Configuration;
using Microsoft.Dnx.Runtime;
using Microsoft.AspNet.Hosting;

namespace Messenger
{
    public class Startup
    {
        public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
        {
            var configurationBuilder = new ConfigurationBuilder(appEnv.ApplicationBasePath)
                .AddJsonFile("config.json")
                .AddEnvironmentVariables();
            Configuration = configurationBuilder.Build();
        }

        public IConfiguration Configuration { get; set; }
    }
...

}

Hope it helps.希望能帮助到你。

Under project.json you will need to add it within dependencies在 project.json 下,您需要将其添加到依赖项中

dependencies {
"Microsoft.Extensions.Configuration.Json": "1.0.0"
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 为什么Visual Studio告诉我数据库不存在而存在? - Why does Visual Studio tell me that a database exists when it does not? 为什么Visual Studio没有告诉我错误在视图中? - Why didn't Visual Studio tell me the error was in the view? 为什么Visual Studio会提示我不要将`this`关键字用于实例变量? - Why does Visual Studio prompt me NOT to use the `this` keyword for instance variables? 为什么Visual Studio会给我这种预期的错误类型 - Why does visual studio give me this error type expected 为什么 SQLite 告诉我“没有当前行”? - Why does SQLite tell me "No current row"? 为什么 Resharper 告诉我可能存在 NullReferenceException? - Why does Resharper tell me there is a possible NullReferenceException? Visual Studio可以告诉我哪个引用引发了NullReferenceException? - Can Visual Studio tell me which reference threw a NullReferenceException? “ConfigurationBuilder”不包含“AddJsonFile”的定义 - 'ConfigurationBuilder' does not contain a definition for 'AddJsonFile' 为什么visual studio中的extract方法命令会创建静态方法? - Why does the extract method command in visual studio create static methods? 为什么这在 Visual Studio 2019 中运行而不是 2022? c#中的方法 - Why does this run in Visual Studio 2019 but not 2022? Method in c#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM