.NET 6 Minimal API无法读取Azure Web App应用设置求助
.NET 6 Minimal API部署到Azure Web App后无法读取应用设置/连接字符串
我开发的.NET 6 Minimal API部署到Azure Web App后,无法读取Azure上配置的应用设置或连接字符串来覆盖本地配置。尝试过环境变量方式,仅能获取字符串值;常规配置读取方法在Azure环境下也不生效。本地调试时能正确读取appsettings.development.json的值,部署到Azure后却返回appsettings.json里的默认值。
本地配置文件
appsettings.json
{ "ConnectionStrings": { "Context": "", "Redis": "" }, "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "Where": "Still AppSettings..." }
appsettings.development.json
{ "ConnectionStrings": { "Context": "...", "Redis": "..." }, "Logging": { "LogLevel": { "Default": "Information", "Microsoft": "Warning", "Microsoft.Hosting.Lifetime": "Information" } }, "Where": "Local", }
API端点代码
Home.cs中定义的端点逻辑:
namespace Endpoints{ public static class HomeApi { public static void RegisterHomeAPI(this WebApplication app) { app.MapGet("/", Index); app.MapGet("/Home", Index); app.MapGet("/Home/Inxed", Index); app.MapPost("/", Index); app.MapPost("/Home", Index); app.MapPost("/Home/Inxed", Index); } // GET: Home public static string Index(IConfiguration configuration) => configuration["Where"] ?? "Failed"; } }
本地调试访问端点返回Local,部署到Azure后返回Still AppSettings...。
Program.cs中的配置代码
using DocumentFormat.OpenXml.InkML; using Microsoft.OpenApi.Models; using Twilio.Services; using Twilio.Web.Endpoints; var builder = WebApplication.CreateBuilder(args); builder.Services.AddLogging(); builder.Services.AddApplicationInsightsTelemetry(options => { options.ConnectionString = builder.Configuration.GetSection("ApplicationInsights").GetValue<string>("InstrumentationKey"); }); builder.Services.AddEndpointsApiExplorer(); builder.Services.AddSwaggerGen(c =>{ c.SwaggerDoc("v1", new OpenApiInfo { Version = "v1", Title = "TEST API v1", Description = "TEST API documentation", Contact = new OpenApiContact() { Name = "CCS Dev", Email = "testing..." } }); }); var config = new ConfigurationBuilder() .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true) .AddEnvironmentVariables() .Build(); var appSettingValue = config["Where"]; var connection = config.GetConnectionString("Context"); builder.Services.AddScoped(c => new TestContext(builder.Configuration["Context"])); builder.Services.AddStackExchangeRedisCache(options => { options.Configuration = builder.Configuration.GetConnectionString("Redis"); }); var appSettings = builder.Configuration.GetSection("AppSettings"); builder.Services.AddScoped(c => new TestService( c.GetRequiredService<TestContext>(), c.GetRequiredService<ILogger<TestService>>() )); builder.Services.AddAuthorization(); var app = builder.Build(); if (app.Environment.IsDevelopment()){} app.UseRouting(); app.UseAuthorization(); app.RegisterHomeAPI(); app.UseSwagger(); app.UseSwaggerUI(); app.Run();
Azure Web App配置

已查阅多篇博客及微软官方文档,问题仍未解决,寻求帮助。
内容的提问来源于stack exchange,提问作者Alex Houser
相关产品推荐
相关产品推荐

