如何在 ASP.NET .NET 7 中以最小 api 读取配置值

How to read configuration value in minimal api in ASP.NET .NET 7

我有现有代码,我正在从 .NET5 升级到 .NET 6

目前我喜欢

ApplicationInsightsServiceOptions aiOptions = new ApplicationInsightsServiceOptions();
aiOptions.EnableAdaptiveSampling = false;
aiOptions.InstrumentationKey = Configuration["ApplicationInsights:InstrumentationKey"];
aiOptions.EnableQuickPulseMetricStream = true;
builder.Services.AddApplicationInsightsTelemetry();

如何注入配置依赖以最小化使用?

  aiOptions.InstrumentationKey = Configuration["ApplicationInsights:InstrumentationKey"];

假设您有权访问:var builder = WebApplication.CreateBuilder(args);

只需使用:

builder.Configuration.GetSection("ApplicationInsights").["InstrumentationKey"];

builder.Configuration["ApplicationInsights:InstrumentationKey"]