I am trying to convert from .NET Core 5 to .NET Core 6.
In my .NET Core 5, Startup.cs, I have
public class Startup
{
public IConfiguration Configuration { get; }
public Startup(IConfiguration configuration)
{
Configuration = configuration;
}
public void ConfigureServices(IServiceCollection services)
{
// ...
Configuration.GetSection("sectionName")
}
}
In .NET Core 6, Startup.cs is removed. And from https://learn.microsoft.com/en-us/aspnet/core/migration/50-to-60?view=aspnetcore-6.0&tabs=visual-studio , I don't see how can I read Configuration section from configuration file, appsettings.json.
Can you please point me where I can find example to do that in .NET 6?
In .Net 6 , Configuration has been configured in Program.cs, You just need to use:
var builder = WebApplication.CreateBuilder(args);
//.......
builder.Services.Configure<Your model>(configuration.GetSection("sectionName"));
to Map appSettings.json object to class.
Or you can just use:
var builder = WebApplication.CreateBuilder(args);
//.......
builder.Configuration.GetSection("sectionName")
to read the value in appsetting.json.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With