简体   繁体   English

如何在ASP.NET Core 6.0 Web API中读取Azure App服务的应用设置

[英]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 , addWeatherForecast.cs中,添加

 public string? myval { get; set; }

Deployed App Service Output:已部署的应用服务 Output:

在此处输入图像描述

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 将ASP.NET Core部署到Azure Web App服务错误 - Deploy ASP.NET Core to Azure Web App Service Error ASP.NET Core 3 Web应用程序,Azure App Service和ANCM进程内处理程序加载失败 - ASP.NET Core 3 Web Application, Azure App Service, and a ANCM In-Process Handler Load Failure 当托管在Azure应用程序服务中时,如何控制添加到ASP.NET核心Web应用程序中的HTTP响应标头? - How can I control the HTTP response headers added to my ASP.NET core web application when hosted in Azure app service? 使用 ASP.NET Core 6.0 中的中间件将 Auth0 用户 ID 传递到服务层 Web API - Passing Auth0 User Id to service layer using middelware in an ASP.NET Core 6.0 Web API 如何将 Blazor Web 程序集与 ASP.NET 核心托管和身份验证应用程序发布到 ZCF04A02E37B774FC3917A 服务? - How to publish Blazor Web assembly with ASP.NET core hosting and identity auth app to azure App Service? .NET核心Web API作为Azure应用程序服务上的虚拟应用程序 - .NET core web API as virtual application on Azure app service 无法发布 ASP.NET Web API 到 Z3A580F142203677F1FZ3 应用服务 - Cannot publish ASP.NET Web API to Azure App Service 使用 ASP.NET 内核 6.0 Web ZDB974238714CA8DE634A7CE1D08 发送 email - Sending email using ASP.NET Core 6.0 Web API AllowAnonymous 不适用于 ASP.NET 内核 6.0 Web API - AllowAnonymous not working with ASP.NET Core 6.0 Web API Azure上的Asp.Net Core Web API - Asp.Net Core Web API on Azure
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM