Home > database >  ASP.NET Core WebAPI 6 cannot get value from configuration, how to fix?
ASP.NET Core WebAPI 6 cannot get value from configuration, how to fix?

Time:09-11

I am trying migration source code of a ASP.NET Core 3.x to version 6.x . Source code enter image description here

How to fix?

CodePudding user response:

You can't inject services in Program.cs using constructor. This is where the application starts, dependency injection container is not created yet. Instead you can access configuration using builder.Configuration:

var builder = WebApplication.CreateBuilder(args);

builder.Services.Configure<AppSettings>(builder.Configuration.GetSection("AppSettings"));

var app = builder.Build();

https://docs.microsoft.com/en-us/aspnet/core/fundamentals/configuration/?view=aspnetcore-6.0#access-configuration-in-programcs

  • Related