Introduction
AppSettings are going to vary from instance to instance, there will be different app settings for the dev environment & different app settings for production environments. If we are deploying the applications to a different customer, app settings will be customer-specific. The best example is if we have an email notification component in your application. To & From emails are usually read from app settings, and these are going to change when the application is deployed to production environments. In these cases, it is important to read the app settings from Azure.
In this article, I'm going the explain how to read the app settings from Azure in .NET Core. Let's go step by step.
Step 1 - Create Web API Project in .NET Core
In Visual Studio, click on File, go to New, and select the appropriate project template.
Click on "Next", enter the project name and create.
It creates a project and structure that looks like this.
In Appsettings.json, add Key value
- "TestKey": "I'm Loading from Local App settings"
Right-click & add a new class called "AppConfig" & interface "IAppConfig".
- public class AppConfig: IAppConfig
- {
- public readonly string _testvalue = string.Empty;
-
- public IConfiguration Configuration { get; }
- public AppConfig(IConfiguration configuration)
- {
- Configuration = configuration;
- _testvalue = Configuration["TestKey"];
- }
-
- public string GetTestValue()
- {
- return _testvalue;
- }
- }
- public interface IAppConfig
- {
- string GetTestValue();
- }
Add this dependency to the startup.cs file.
- services.AddTransient<IAppConfig, AppConfig>();
Go to Controller & inject "IAppConfig"
- private readonly IAppConfig _appconfig;
-
- public WeatherForecastController(IAppConfig appconfig)
- {
- _appconfig = appconfig;
- }
-
- [HttpGet]
- public IActionResult Get()
- {
- var result = _appconfig.GetTestValue();
- return Ok(result);
- }
If you run this project you will get the below response.
Step 2 - Create WebApp in Azure & Deploy
Login to your Azure account, in the home page, we have the "App Services" option, click on that to create a new Azure app service.
It will take you to create a new app service page, fill in all the details and create.
It will take few seconds of time to deploy. After deploying, click on the go-to-resources.
In the left side menu, click on the search box & search for configuration, click on the configuration & click on "New Application Settings", add pair of key-value which we used in our appsettings.json. Click on "save", it might take 5-10 secs to deploy the changes.
"TestKey": "I'm Loading from Azure".
Download the published profile from azure & publish our .Net Core Web API project.
Step 3 - Run & Test
Azure
It's working.
How to handle if we have nested items in appsettings.json
Add nested items in the appsettings.json & modify the key name in the .cs file.
- "Main": {
- "Inner": "I'm inner value from Local App settings"
- }
- public AppConfig(IConfiguration configuration)
- {
- Configuration = configuration;
- _testvalue = Configuration["Main:Inner"];
- }
Next, Go to Azure & add new app settings in the configuration section of the app service. If it's nested, we need to use "
:" as a separator.
Deploy & Check response
Local Response
Azure Response
It's working.
Summary
In this article, I explained how to read the app settings from Azure. Hope it's helpful.