[英]How to read application setting of Azure App service in ASP.NET Core 6.0 Web API
I am working on a .NET Core 6.0 "Hello World" app and it'll be deployed as an Azure web service.我正在开发 .NET Core 6.0“Hello World”应用程序,它将作为 Azure web 服务进行部署。
I am trying to read value from Azure App Service -> Configuration -> ApplicationSetting -> mykey, but I am not able to access value of mykey
after deploying to app service.我正在尝试从 Azure App Service -> Configuration -> ApplicationSetting -> mykey 读取值,但在部署到应用服务后我无法访问
mykey
的值。 I am able to read value of mykey
in local while debugging.调试时,我能够在本地读取
mykey
的值。
Project type:项目类型:
I have to read mykey
in Program.cs
.我必须阅读
Program.cs
中的mykey
。 What is the simplest way to read the value of mykey
since it is just a hello world app?读取
mykey
值的最简单方法是什么,因为它只是一个 hello world 应用程序?
Program.cs
: Program.cs
:
using HelloWorldApi.Helpers;
using Microsoft.AspNetCore.Authentication.Certificate;
using Microsoft.AspNetCore.Server.Kestrel.Https;
var builder = WebApplication.CreateBuilder(args);
builder.Services.AddControllers();
builder.Services.AddEndpointsApiExplorer();
var provider = builder.Services.BuildServiceProvider();
var configuration = provider.GetRequiredService<IConfiguration>();
builder.WebHost.ConfigureKestrel(o =>
{
o.ConfigureHttpsDefaults(o =>
{
});
}).ConfigureAppConfiguration((hostContext, builder) =>
{
var settings = builder.Build();
// I tried different ways but not getting value of mykey.
// This code is returning null
var connectionString = configuration.GetValue<string>("mykey");
});
var app = builder.Build();
Please check the below changes in your code to read the values from Azure App Settings.请检查代码中的以下更改,以读取 Azure 应用程序设置中的值。
In Program.cs
,在
Program.cs
中,
Retrieving values with your code.使用您的代码检索值。
var AppsetVal = configuration.GetValue<string>("mykey")
OR或者
From my Sample code从我的示例代码
var AppsetVal = builder.Configuration.GetValue<string>("mykey");
My appsettings.json
file:我的
appsettings.json
文件:
{
"Logging": {
"LogLevel": {
"Default": "Information",
"Microsoft.AspNetCore": "Warning"
}
},
"AllowedHosts": "*",
"mykey": "test"
}
Settings in Azure: Azure中设置:
Here Iam displaying the value in API Controller.这里我显示 API Controller 中的值。
In WeatherForecastController.cs:在 WeatherForecastController.cs 中:
using Microsoft.AspNetCore.Mvc;
namespace WebApplication7.Controllers
{
[ApiController]
[Route("[controller]")]
public class WeatherForecastController : ControllerBase
{
private readonly ILogger<WeatherForecastController> _logger;
private readonly IConfiguration Configuration;
public WeatherForecastController(ILogger<WeatherForecastController> logger, IConfiguration configuration)
{
_logger = logger;
Configuration = configuration;
}
[HttpGet(Name = "GetWeatherForecast")]
public IEnumerable<WeatherForecast> Get()
{
//var MyAppSettings = Configuration.GetSection("AppSettings");
return Enumerable.Range(1, 5).Select(index => new WeatherForecast
{
Date = DateTime.Now.AddDays(index),
TemperatureC = Random.Shared.Next(-20, 55),
Summary = Summaries[Random.Shared.Next(Summaries.Length)],
myval = Configuration.GetSection("mykey").Value
})
.ToArray();
}
}
}
In WeatherForecast.cs
, add在
WeatherForecast.cs
中,添加
public string? myval { get; set; }
Deployed App Service Output:已部署的应用服务 Output:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.