F11
F11

Reputation: 3806

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.

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. I am able to read value of mykey in local while debugging.

enter image description here

Project type:

This is my

I have to read mykey in Program.cs. What is the simplest way to read the value of mykey since it is just a hello world app?

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();

enter image description here

Upvotes: 3

Views: 1815

Answers (1)

Harshitha
Harshitha

Reputation: 7297

Please check the below changes in your code to read the values from Azure App Settings.

In 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:

{
  "Logging": {
    "LogLevel": {
      "Default": "Information",
      "Microsoft.AspNetCore": "Warning"
    }
  },
  "AllowedHosts": "*", 
  "mykey": "test"
}

Settings in Azure: enter image description here

Here Iam displaying the value in API Controller.

In 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

 public string? myval { get; set; }

Deployed App Service Output:

enter image description here

Upvotes: 4

Related Questions