我正在使用 Visual Studio 2022 構建一個 asp.net core v6 應用程式。
我需要在 Azure Ass Services 實體上部署和運行我的應用程式。
我的應用程式appsettings.json檔案中有許多設定。該檔案包含在部署包中,如下所示:

我使用 Visual Studio 2022 發布功能將我的 asp.net 核心應用程式部署到 Azure 應用服務實體:

成功發布 Visual Studio 后,appsettings.json配置刀片中不會顯示任何檔案設定:

問題:
1-Visual Studio 有沒有辦法將功能部署appsettings.json設定發布到應用服務的配置刀片?
2-即使部署成功,我的應用程式也不會讀取appsettings.json. 似乎都忽略了。我檢查并在我的本地 Visual Studio 開發人員環境中,我的應用程式讀取 appsettings.json,但不是在部署到應用程式服務時。為什么部署的應用程式會忽略appsettings.json設定?
更新1:program.cs和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可以是任何東西。只需要使用一個ConfigurationManager實體來讀取它的內容,就像我在站點在 Visual Studio 中本地運行時讀取它們的方式一樣。
uj5u.com熱心網友回復:
- 有沒有辦法 Visual Studio 發布功能將 appsettings.json` 設定部署到應用服務的配置刀片?
部署的appsettings.json 檔案將在 KUDU 除錯控制臺中可用。
在你的Azure App Service=> Advanced Tools=> Go=> Debug Console=> cmd=> site=>wwwroot中。

- 甚至您可以
KUDU使用以下 URL打開
https://YourAppServiceName.scm.azurewebsites.net/
配置部分不會顯示
appsettings.json檔案中的設定。它用于檔案中override的可用設定appsettings.json。override必須在Application Settingsin 門戶中設定您想要的值和要在部署后添加的新值。appsettings.json和Configuration=>Application設定中的鍵名必須相同。

我的示例 appsettings.json :
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"ConnectionStrings": {
"MyVal": "Local Connection"
},
"AppSettings": {
"MySettings": "Local Settings"
}
}
從 appsettings.json 檢索值的代碼
var MyAppSettings = builder.Configuration.GetSection("AppSettings");
var LocalAppset = MyAppSettings.GetValue<string>("MySettings");
或者
var MyAppSettings1 = builder.Configuration.GetValue<string>("AppSettings:MySettings");
索引.cshtml
@page
@model IndexModel
@using Microsoft.Extensions.Configuration
@inject IConfiguration Configuration
<h2>Configuration value for 'AppSettings': @Configuration["AppSettings:MySettings"]</h2>
- 我已經部署了應用程式并且能夠從部署的代碼中讀取值。
初始值

被覆寫的值

轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/532128.html
