In ASP.NET Web API, the configuration settings for your application, such as connection strings, API keys, and other application-specific settings, can be stored in the appsettings.json file.
This file is typically located in the root directory of your project.
In your Web API project, install the Microsoft.Extensions.Configuration NuGet package if it’s not already installed.
This file is typically located in the root directory of your project.
In your Web API project, install the Microsoft.Extensions.Configuration NuGet package if it’s not already installed.
It should have a JSON structure with key-value pairs representing your settings.
For example,
{
"ConnectionStrings": {
"DefaultConnection": "Data Source=...;Initial Catalog=...;User Id=...;Password=..."
},
"AppSettings": {
"ApiKey": "your-api-key"
}
The appsettings in Asp.net core have different configuration sources as shown below.
- appsettings.json File
- Environment Variable
- User Secrets
- Command Line Arguments
Read Values from appsettings.json:
In your Program.cs add the following code to read the appsettings.
var apiKey = builder.Configuration.GetValue<string>("AppSettings:ApiKey");
We are directly getting the string type value from the configuration object and separating our nested sections by “:”.
In the above code, you can inject the app settings into your services or controllers through constructor injection(Dependency Injection type).
private string _apikey;
public AppController(string apikey)
{
_apikey = apikey;
}
Environment:
When your developing an application, it’s common to have different configurations for different environments
⦁ development,
⦁ staging,
⦁ production.
In ASP.NET Web API, you can easily manage environment-specific app settings by utilizing the appsettings.json file along with environment-specific configuration files.
To access these app settings in your Web API application, you can use the Configuration object provided by the ASP.NET Core framework.
Create separate configuration files for each environment:
⦁ appsettings.Development.json for the development environment
⦁ appsettings.Staging.json for the staging environment
⦁ appsettings.Production.json for the production environment
⦁ appsettings.qa(QA).json for the QA environment
Each file should contain the specific app settings for its respective environment.
To Run Different Environment:
To run your ASP.NET Web API application in a different environment, you need to set the appropriate environment variable or modify the launch settings.
Setting Environment Variable:
One way to specify the environment is by setting an environment variable. The most common environment variable used in ASP.NET Core applications is ASPNETCORE_ENVIRONMENT.
You can set this variable to the desired environment value before running your application.
For Windows (Command Prompt):
set ASPNETCORE_ENVIRONMENT=Development
dotnet run
Replace Development with the desired environment name, such as Staging or Production.
Modifying Launch Settings:
Another approach is to modify the launch settings of your project. Launch settings allow you to configure different profiles for different environments. Here’s how you can modify the launch settings:
Open the Properties folder in your project –> launchSettings.json file.
Inside the file, you’ll find profiles for different environments (e.g., Development, Staging, Production). Modify the environmentVariables section of the desired profile to set the ASPNETCORE_ENVIRONMENT variable.
{
"$schema": "https://json.schemastore.org/launchsettings.json",
"iisSettings": {
"windowsAuthentication": false,
"anonymousAuthentication": true,
"iisExpress": {
"applicationUrl": "http://localhost:16731",
"sslPort": 44321
}
},
"profiles": {
"Appset": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7179;http://localhost:5003",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
},
"IIS Express": {
"commandName": "IISExpress",
"launchBrowser": true,
"launchUrl": "swagger",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "Development"
}
}
}
}
Save the file, and when you run your application using Visual Studio or the command line, it will use the specified environment based on the selected launch profile.
Using either of these methods, you can run your ASP.NET Web API application in different environments by setting the appropriate environment variable or modifying the launch settings.
After the configuration, Create a action method to retrieve a data from appsettings file,
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
namespace Appset.Controllers
{
[Route("api/[controller]")]
[ApiController]
public class AppController : ControllerBase
{
private string _apikey;
public AppController(string apikey)
{
_apikey = apikey;
}
[HttpGet]
public ActionResult Index()
{
return Ok(new { _apikey });
}
}
}
its a sample controller to retrieve a data from appsettings.json.
Defining a Get action method to return a value which we declared in Appsettings.json.
if suppose we did not give any values to any of the environment appsettings like appsetting.Development.json. It will overwrite a value to environment appsettings based on the launch settings.
"profiles": {
"Appset": {
"commandName": "Project",
"dotnetRunMessages": true,
"launchBrowser": true,
"launchUrl": "swagger",
"applicationUrl": "https://localhost:7179;http://localhost:5003",
"environmentVariables": {
"ASPNETCORE_ENVIRONMENT": "QA"
}
From above code environmentVariable is QA .but in appsettings.QA.json has no value to retrieve . So it will overwrite the values.
before running the application,you have to check whether app.Environment.IsDevelopment() this line has NOT operator or not in Program.Cs
if (app.Environment.IsDevelopment())
{
app.UseSwagger();
app.UseSwaggerUI();
}
if you want to run this app in QA Environment or any environment you have to check whether the NOT operator is marked .
if (!app.Environment.IsDevelopment())
after doing these process run the application.

In the above figure, the data is retrieved from appsetting file and the value is displayed.
Note: We ran this appication in QA environment.
Overall, you can efficiently manage and access configuration settings for different environments in your ASP.NET Web API application.
No Comment! Be the first one.