在 MVC6 中读取 config.json 时抛出 NullReferenceException

NullReferenceException thrown while reading config.json in MVC6

我正在开发 MVC6 网络应用程序。我的 Startup.cs 有以下代码-

public class Startup
{
    public static Microsoft.Framework.ConfigurationModel.IConfiguration Configuration { get; set; }

    public Startup(IHostingEnvironment env)
    {
       //following line throws NullReferenceException
       Configuration = new Configuration().AddJsonFile("config.json").AddEnvironmentVariables();
    }
}

config.json-

{
    "Data": {
        "DefaultConnection": {
            "ConnectionString": "Server=(localdb)\MSSQLLocalDB;Database=_CHANGE_ME;Trusted_Connection=True;"
         }
     }
}

有什么帮助吗?

UPDATE: This question is not all about NullReferenceException. In ASP.NET-5 MVC-6, config.json is a new addition. I am using the code as it is found in several blogs. Here are few links-

如果您使用的是 Visual Studio 的 RTM 版本,则必须使用 asp.net 的 beta5 修订版。在此版本中,配置的命名空间已更改。

编辑: 你必须添加这个包:Microsoft.Framework.Configuration.Json

此代码一定适合您:

using Microsoft.Framework.Configuration;
using Microsoft.Framework.Runtime;

namespace Test
{
    public class Startup
    {
        public IConfiguration Configuration { get; set; }

        public Startup(IHostingEnvironment env, IApplicationEnvironment appEnv)
        {
            var builder = new ConfigurationBuilder(appEnv.ApplicationBasePath)
                .AddJsonFile("config.json")
                .AddEnvironmentVariables();

            Configuration = builder.Build();
        }
    }
}