Using ASP.NET Core's ConfigurationBuilder in a Test Project

后端 未结 4 1596
天涯浪人
天涯浪人 2021-02-13 03:21

I want to use the IHostingEnvironment and ConfigurationBuilder in my functional test project, so that depending on the environment the functional tests

4条回答
  •  醉酒成梦
    2021-02-13 04:21

    This is possible, according to the ASP.NET Core documentation (http://docs.asp.net/en/latest/fundamentals/configuration.html) you can build this inside the Startup method and add the Environment variable appSettings. When specifing the optional flag you can use the appSettings.Production.json for production environment and appSettings.json for development when there is no appSettings.Development.json available. The appsettings.{env.EnvironmentName}.json is only used when the file is present and the fallback is the default appsettings.json

    public Startup(IHostingEnvironment env)
    {
        // Set up configuration providers.
        var builder = new ConfigurationBuilder()
            .AddJsonFile("appsettings.json")
            .AddJsonFile($"appsettings.{env.EnvironmentName}.json", optional: true);
    
        if (env.IsDevelopment())
        {
            // For more details on using the user secret store see http://go.microsoft.com/fwlink/?LinkID=532709
            builder.AddUserSecrets();
        }
    
        builder.AddEnvironmentVariables();
        Configuration = builder.Build();
    }
    

提交回复
热议问题