New .Net Core 2 Site does not reconize Configuration.GetConnectionString

1# install the NuGet package: Microsoft.Extensions.Configuration
2# add: using Microsoft.Extensions.Configuration;
3# Note that i have added this line in the code: public IConfiguration Configuration { get; }

 public class Startup
    {
        public Startup(IConfiguration configuration)
        {
            Configuration = configuration;
        }
        public IConfiguration Configuration { get; }

        public void ConfigureServices(IServiceCollection services)
        {
            services.AddControllers();
        }

        public void Configure(IApplicationBuilder app, IWebHostEnvironment env)
        {
            if (env.IsDevelopment())
            {
                app.UseDeveloperExceptionPage();
            }
            app.UseHttpsRedirection();
            app.UseRouting();
            app.UseAuthorization();

            app.UseEndpoints(endpoints =>
            {
                endpoints.MapControllers(); 
            });
        }
    }

You need to get the IConfiguration object via DI.
Add a IConfiguration argument to your Startup's constructor, and assign it to a Configuration property:

public Startup(IConfiguration configuration)
{
    Configuration = configuration;
}

public IConfiguration Configuration { get; }

I'm surprised how you don't have it though, because it's part of the template.


Sometimes you add the wrong namespace like AutoMapper.Configuration in place of the correct one Microsoft.Extensions.Configuration.