Is ConfigurationManager.AppSettings available in .NET Core 2.0?

前端 未结 5 1736
孤街浪徒
孤街浪徒 2020-11-29 01:05

I\'ve got a method that reads settings from my config file like this:

var value = ConfigurationManager.AppSettings[key];

It compiles fine

5条回答
  •  死守一世寂寞
    2020-11-29 01:25

    You can use Configuration to resolve this.

    Ex (Startup.cs):

    You can pass by DI to the controllers after this implementation.

    public class Startup
    {
        public Startup(IHostingEnvironment env)
        {
            var builder = new ConfigurationBuilder()
            .SetBasePath(env.ContentRootPath)
            .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true);
    
            Configuration = builder.Build();
    
        }
    
        public IConfiguration Configuration { get; }
    
        // This method gets called by the runtime. Use this method to add services to the container.
        public void ConfigureServices(IServiceCollection services)
        {
    
            var microserviceName = Configuration["microserviceName"];
    
           services.AddSingleton(Configuration);
    
           ...
        }
    

提交回复
热议问题