Assume that you are working in a company which maintains different environments, like Development, QA, Staging etc. Now, many times, it happens that a portion of the code is working in one environment and the same code is not working in another environment. In this situation, you need to configure the other environment locally and you need to modify each and every configuration, such as - connectionstring, url, directorypath etc. So, in order to make the developer’s life easy, we can maintain the environment specific configurations and load accordingly.
Let’s see step by step how we can use this feature in .NET Core Web API.
- Open Visual Studio and create a new project.
- Select API template and click the OK button.
Create two different configuration files, i.e., JSON files - development.json and staging.json; both having different parameter values.
development.json
- {
- "Configuration": {
- "ConnectionString": "Server={devservername};Database={dbname};User ID={devuserid};Password={devpassword};"
- }
- }
staging.json
- {
- "Configuration": {
- "ConnectionString": "Server={stagingservername};Database={dbname};User ID={staginguserid};Password={stagingpassword};"
- }
- }
Generate a model matching with your config files.
- namespace EnvironmentSpecificCustomConfig
- {
- public class Configuration
- {
- public string ConnectionString { get; set; }
- }
- }
In the startup class constructor, we need to add a JSON whose name should be the value of the parameter EnvironmentName which we get through IHostingEnvironment.
Add a Configuration class to the service collection so that we can inject it whenever it’s required.
- using Microsoft.AspNetCore.Builder;
- using Microsoft.AspNetCore.Hosting;
- using Microsoft.Extensions.Configuration;
- using Microsoft.Extensions.DependencyInjection;
- namespace EnvironmentSpecificCustomConfig
- {
- public class Startup
- {
- public Startup(IConfiguration configuration, IHostingEnvironment env)
- {
- var builder = new ConfigurationBuilder()
- .SetBasePath(env.ContentRootPath)
- .AddJsonFile("appsettings.json", optional: true, reloadOnChange: true)
- .AddJsonFile($"Config/{env.EnvironmentName}.json", optional: false, reloadOnChange: true)
- .AddEnvironmentVariables();
- configuration = builder.Build();
- Configuration = configuration;
- }
- public IConfiguration Configuration { get; }
- // This method gets called by the runtime. Use this method to add services to the container.
- public void ConfigureServices(IServiceCollection services)
- {
- services.AddSingleton(Configuration.GetSection("Configuration").Get<Configuration>());
- services.AddMvc();
- }
- // This method gets called by the runtime. Use this method to configure the HTTP request pipeline.
- public void Configure(IApplicationBuilder app, IHostingEnvironment env)
- {
- if (env.IsDevelopment())
- {
- app.UseDeveloperExceptionPage();
- }
- app.UseMvc();
- }
- }
- }

Jeeva SubburajPosted Jan 30, 2019, 4:06 PM
Thanks Akshay.. i have done similar article.. in that, i mentioned it load it from user secrets and command line argument and azure key valul as well. https://www.c-sharpcorner.com/article/asp-net-core-basics-accessing-configuration-settings-from-multiple-sources/