I am using Visual Studio 2022 to build an asp.net core v6 application.
I need to deploy and run my application on an Azure Ass Services instance.
I have many settings in my application's appsettings.json
file. The file is included in the deployment package as shown here:
I use Visual Studio 2022 Publish feature to deploy my asp.net core application to an Azure App Services instance:
After a successful Visual Studio Publish, none of appsettings.json
file settings show in the configuration blade:
Questions:
1- Is there any way Visual Studio publish feature deploy appsettings.json
settings to the App Services' configuration blade?
2- Even after a successful deployment, my application does not read the settings in the appsettings.json
. It seems that all ignored. I checked and on my local visual studio developer environment, my app reads appsettings.json
, but not when it is deployed to App Services. Why the deployed app ignores the appsettings.json
settings?
Update 1: program.cs
and appsettings.json
public static void Main(string[] args)
{
var builder = WebApplication.CreateBuilder(args);
builder.Logging.ClearProviders();
builder.Logging.AddConsole();
builder.Logging.AddAzureWebAppDiagnostics();
// Add services to the container.
builder.Services.AddAuthentication(JwtBearerDefaults.AuthenticationScheme)
.AddMicrosoftIdentityWebApi(builder.Configuration.GetSection("AzureAd"));
builder.Services.AddControllers();
// Learn more about configuring Swagger/OpenAPI at https://aka.ms/aspnetcore/swashbuckle
builder.Services.AddEndpointsApiExplorer();
builder.Services.AddSwaggerGen();
builder.Services.AddApplicationInsightsTelemetry();
builder.Services.AddScoped<Framework>();
var app = builder.Build();
// Configure the HTTP request pipeline.
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
app.UseHttpsRedirection();
app.UseAuthentication();
app.UseAuthorization();
app.MapControllers();
app.Run();
}
appsettings.json
could be anything. Just need to read its content with a ConfigurationManager
instance like how I read them when the site is running locally in Visual Studio.
CodePudding user response:
- Is there any way Visual Studio publish feature deploy appsettings.json` settings to the App Services' configuration blade?
The deployed appsettings.json
file will be available in KUDU Debug Console.
In your Azure App Service
=> Advanced Tools
=> Go
=> Debug Console
=> cmd
=> site
=> wwwroot
.
- Or even you can open the
KUDU
with the below URL
https://YourAppServiceName.scm.azurewebsites.net/
Configuration section will not display the setting which are in
appsettings.json
file. It is used tooverride
the available settings fromappsettings.json
file.The values which you want to
override
and the new values which you want to add after deployment must be set in theApplication Settings
in portal.The key name must be same in both
appsettings.json
andConfiguration
=>Application
Settings.
My sample appsettings.json :
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"MyVal": "Local Connection"
},
"AppSettings": {
"MySettings": "Local Settings"
}
}
Code to retrieve values from appsettings.json
var MyAppSettings = builder.Configuration.GetSection("AppSettings");
var LocalAppset = MyAppSettings.GetValue<string>("MySettings");
OR
var MyAppSettings1 = builder.Configuration.GetValue<string>("AppSettings:MySettings");
Index.cshtml
@page
@model IndexModel
@using Microsoft.Extensions.Configuration
@inject IConfiguration Configuration
<h2>Configuration value for 'AppSettings': @Configuration["AppSettings:MySettings"]</h2>
- I have deployed the app and able to read the values from the deployed code.
Initial Values
Overridden Values